This is all you are probably missing:
Class Form1
Private f2 As Form2 ' this is Form1's reference to the
' form2 instance
Later when you click to go to form2, your original code just needs a small tweak:
If f2 Is Nothing Then
f2 = New Form2(Me) ' set declared variable to new instance
End If
F2.Show()
Me.Hide()
In this case Form1 is passing the reference using the trick you were shown before using the constructor:
Sub New(frm As Form1) ' this is in Form2 only
f1 = frm
End Sub
You dont need this in Form1 because he/it is creating his own f2 object reference.
The main problem in your original code, was: Dim Form2 As New Form2
. You are creating a new Form2 each time (I suspect that resides in an event or sub). Those new instances
can't know the control values in the previous instances. Declaring F1
or F2
as shown gives it module/form level Scope
.
Dim
declares a variable and its Type. f1 is of Type Form1. It does not create an object if it is an object variable
New
creates an instance of an object Type (reference types). This directly relates to the Sub New
method in the class. When you use New
, Sub New
is called so anything special that is needed can take place there. Value types like Integer
do not need to be created or instanced, only declared.
Where you declare (Dim
) a variable determines its Scope
. If you do this in a Sub, the variable or object only exists in that sub. if you do it at the form/class level, it has Form/Class level scope.