Link to home
Start Free TrialLog in
Avatar of KalluMama
KalluMama

asked on

Transfer data between two forms...

I am just learning VB.net and had a question. If i have two forms and want to transfer some data between the two forms; is using the module the best way to do this?
Is that the recomended way or the only way? thanks

Avatar of jerete
jerete

You don't really have to use a module, you can reference the data using the name of the form. For example:

  Form1.TextBox1.Text = Form2.TextBox2.Text

Avatar of KalluMama

ASKER

...but is using modules the recommended practice?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Extending on Idle_Minds solution, if the two forms are independent then have a reference to each form global so you can access them from anywhere in your code.


'Define globally
Public Form1_Pointer as Form1
Public Form2_Pointer as Form2

'Define something like this, sounds like you have this part figured out
Public Sub New()
   Form1_Pointer = new Form1
   Form2_Pointer = new Form2

   Form1_Pointer.Show()
   Form2_Pointer.Show()

   Application.Run(Form1_Pointer)

End Sub

You can now access either form from anywhere in the code.  Just make sure that the varibles being accessed in each form are public.

Form1_Pointer.lbl1.Text = Form2_Pointer.lbl2.Text


'Alternatively, if Form 2 is embedded in (a child of) Form 1 and you want to send data back up to Form 1 from Form 2 without referencing Form1 as a global pointer, try the following.

 'In Form 2
CType(CType(Me.Parent, System.Windows.Forms).Parent, Form1).ValueOnForm1 = Value

Hope this helps