By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,240 Members | 782 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,240 IT Pros & Developers. It's quick & easy.

Problem transporting info from 1 form to another

P: n/a
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 Bilheux, PhD
Oak Ridge National Laboratory
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Jean wrote:
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

Jul 21 '05 #2

P: n/a
That was all I need to know.
thanks a lot

"Chris Dunaway" wrote:

Jean wrote:
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

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.