-1

All i want to do is access my property from a parent Form in vb.net Forms. but it won't let me do

 Dim formParent1 As formParent

 Private Sub search()
 formParent1.propertyName = 1
 End Sub

but it gives an error at formParent1.propertyname = 1. Saying "Object reference not set to an instance of an object".

but i may not use

Dim formParent1 as formParent = new formParent()

how can I access this propery, I am new at VB.net best regards

deltu100
  • 581
  • 7
  • 26
  • To save a few keystrokes, you could use `Dim formParent1 as New formParent()` instead. – Laoujin Aug 30 '12 at 15:24
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp May 14 '14 at 21:33

2 Answers2

3

If I understand you correctly, that code is inside a child form of formParent. To use a reference to the current formParent you need to pass inside the child form a reference to this current formParent.

So in the formParent, when you start the execution of the child form, write this:

Dim f as formChild = new formChild(Me)
f.ShowDialog() 

in the form child constructor save the passed reference to the global variable formParent1

Dim formParent1 as formParent ' this is the global level reference to the parentForm'

Public Sub formChild(ByVal f as formParent)
       formParent1 = f
End Sub

now you can use the reference formParent1 inside the child form without initialize another formParent For example, assuming that the formParent1 contains a public property named SearchResult, when you complete your search you call:

Private Sub searchBtn_Click(sender As Object, e As System.EventArgs) Handles SearchButton.Click
     Dim searchResult as String

     ' Here write the code that executes the search and set the value in searchResult variable

     ' Pass the searchResult variable to the parent form via the global reference.
     formParent1.SearchResult = searchResult
End Sub
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I almost thought that was the answer, but apparently doesn't work on an event this way. – deltu100 Aug 30 '12 at 13:49
  • Could you explain me what do you mean 'doensn't work on an event' ? – Steve Aug 30 '12 at 13:51
  • Private Sub SearchBtn_Click(sender As System.Object, e As System.EventArgs) Handles SearchBtn.Click - if I put the argument inbetween that, it says it cant handle it – deltu100 Aug 30 '12 at 13:53
  • Method 'Private Sub searchBtn_Click(sender As Object, e As System.EventArgs, formParent As Basis.ParentForm)' cannot handle event 'Public Event Click(sender As Object, e As System.EventArgs)' because they do not have a compatible signature – deltu100 Aug 30 '12 at 14:02
  • 'Private Sub searchBtn_Click(sender As Object, e As System.EventArgs) Handles SearchButton.Click` then inside the event code use the parentForm. I will update my answer – Steve Aug 30 '12 at 14:12
1

You just need to keep a reference to the form instance in a global variable.

Create a module - in it put:

Private _globalParentForm as parentForm = Nothing

Public ReadOnly Property GlobalParentForm as parentForm
    Get
        If _globalParentForm Is Nothing Then _globalParentForm = New parentForm
        Return _globalParentForm        
    End Get
End Property

Then you can access the parent form reference in your search form:

GlobalParentForm.SearchProperty = searchText
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • there are some load options in the parent, if I use "new" then it won't load in the database datacontext. Because you see, this child form is a searchForm. And I want to send the search.text and option to the parent without having to load in the database again. – deltu100 Aug 30 '12 at 13:39
  • @deltu100 OK I think I understand now what you are trying to do - have a look at my edited answer – Matt Wilko Aug 30 '12 at 13:44
  • Then you must be accessing the form before it is loaded (or else you have unloaded the form). I will update my answer to provide a singleton accessor so this can't happen – Matt Wilko Aug 30 '12 at 15:37