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

KalluMamaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jereteCommented:
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

KalluMamaAuthor Commented:
...but is using modules the recommended practice?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
jerete...that syntax only works in VB.Net 2005.  If you have VB.Net 2003, you need an actual reference to Form1 and Form2...you can't just use their names.

You can...

    (1) Use a Module with Public variables

    (2) Create variables in the Forms and set them:

        Public Class Form1
            ...

            Private Sub Button1_Click(...) Handle Button1.Click
                Dim f2 As New Form2
                f2.Data1 = "Hello "
                f2.Data2 = "World"
                f2.Show()
            End Sub

        End Class

        Public Class Form2
            ...

            Public Data1 As String
            Public Data1 As String

        End Class

    (3) To communicate the other way you can use Events or Pass a reference to the first form in the same manner as (2)

    (4) There are other ways...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
neelbakCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.