user1704812 user1704812 - 4 months ago 13
Vb.net Question

How to reference an existing object in VB.net?

How do you reference an existing object in vb.net?

To describe my problem more specifically, I have my main form

Form1
that loads when I start the application.
Form1
has a datagridview
dgv1
. I have another form
form2
in the project with a bunch of textboxes. On clicking a button on
Form1
I create an instance of
form2
. From
form2
how do I reference the existing
form1
to populate
dgv1
with input from the texboxes on
form2
?

Dai Dai
Answer

You need to pass a reference-to-Form1 to Form2. Use the Me keyword to get a reference to the object currently executing:

In Form1.vb:

Sub Form1_OpenForm2()

    Dim form2 As New Form2()
    form2.AcceptForm1( Me )
    form2.Show()
End Sub

In Form2.vb:

Private _form1 As Form1

Public Sub AcceptForm1(form1 As Form1)
    _form1 = form1
End Sub