Problem transporting info from 1 form to another

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two form and I want to display in the first form some of the info from
the second one. Here is how I tried to do it:

<<<<ok button of form2>>>>>
Dim Myform1 As New Form1
Myform1.Label1.Text = TextBox1.Text
'label1 is in the first form

well, it seems very simple but my program does not want to update the
information of the first form. I guess I'm doing something wrong but what ??

Thank you for your help
 
Jean said:
I have two form and I want to display in the first form some of the info from
the second one. Here is how I tried to do it:

<<<<ok button of form2>>>>>
Dim Myform1 As New Form1
Myform1.Label1.Text = TextBox1.Text

These lines create a brand new Form1 and do not access the text box on
the original form1.


Just like any other class, you need to have a reference to Form1 inside
the second form in order to access its properties or methods. One way
is to have your app start with a Sub Main, then create a global
variable to hold the reference to your first form. Something like
this:

Public MainForm As Form1

Public Sub Main()
MainForm = New Form1
Application.Run(MainForm)
End Sub

Another method is to pass in a reference to Form1 into Form2 using an
overloaded constructor, you can then access the variable throughout
form 2.

<< In button click on Form1 >>

Dim MyForm2 As New Form2(Me)

<< In Form2 >>

Private Form1Reference As Form1

Public Sub Form2(formref As Form1)
Form1Reference = formref
End Sub

Hope this gives you some ideas,

Chris
 
That was all I need to know.
thanks a lot

Chris Dunaway said:
These lines create a brand new Form1 and do not access the text box on
the original form1.


Just like any other class, you need to have a reference to Form1 inside
the second form in order to access its properties or methods. One way
is to have your app start with a Sub Main, then create a global
variable to hold the reference to your first form. Something like
this:

Public MainForm As Form1

Public Sub Main()
MainForm = New Form1
Application.Run(MainForm)
End Sub

Another method is to pass in a reference to Form1 into Form2 using an
overloaded constructor, you can then access the variable throughout
form 2.

<< In button click on Form1 >>

Dim MyForm2 As New Form2(Me)

<< In Form2 >>

Private Form1Reference As Form1

Public Sub Form2(formref As Form1)
Form1Reference = formref
End Sub

Hope this gives you some ideas,

Chris
 
Back
Top