Reference to caller form's public variable/function

jdy0803

Well-known member
Joined
Sep 9, 2012
Messages
73
Location
Santa Clarita
Programming Experience
10+
There' a form(formA) which calls another form(formB).


formA calls formB like this.




Using frm As New formB
frm.ShowDialog(Me)
End Using


In formB, refer to formA's control like this.
CType(Me.Owner, formA).Text1.Text = "AAA"




How do I refer formA's public variable or public function?
Should I use CType(Me.Owner, formA) like control?
CType(Me.Owner, formA).myVar = 1
ret = CType(Me.Owner, formA).myFunc()
or just do like this?
formA.myVar = 1
ret = formA.myFunc()
 
You already are. What do you think 'Text1' is? It's a public variable.

You should only be casting the owner once though not over and over again. You can do something like this:
Private typedOwner As Form1

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    typedOwner = DirectCast(Owner, Form1)
End Sub
You can then use 'typedOwner' everywhere you need to access the caller.
 
Back
Top