Results 1 to 4 of 4

Thread: how do you link variables through forms?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    21

    how do you link variables through forms?

    like i have form 1 and form 2
    and i have a integer on form 1 called x
    how do i get this to show up on form 2?

  2. #2
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: how do you link variables through forms?

    vb.net Code:
    1. ' Form 1 code
    2. Friend x As Integer
    3.  
    4. Private Sub Button1_Click( _
    5.     ByVal sender As System.Object, _
    6.     ByVal e As System.EventArgs _
    7. ) Handles Button1.Click
    8.  
    9.     Dim frm2 As New Form2
    10.     frm2.frm1 = Me
    11.     frm2.ShowDialog()
    12.     frm2.Dispose()
    13. End Sub
    14.  
    15.  
    16. ' Form2 code
    17. Friend frm1 As Form1
    18.  
    19. Private Sub Form2_Load( _
    20.     ByVal sender As System.Object, _
    21.     ByVal e As System.EventArgs _
    22. ) Handles MyBase.Load
    23.  
    24.     MessageBox.Show(frm1.x)
    25. End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how do you link variables through forms?

    Congratulations! You are our 1 millionth customer here at "how do I pass data from one form to another".

    http://www.vbforums.com/showthread.php?t=466253
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how do you link variables through forms?

    Quote Originally Posted by Deepak Sakpal
    vb.net Code:
    1. ' Form 1 code
    2. Friend x As Integer
    3.  
    4. Private Sub Button1_Click( _
    5.     ByVal sender As System.Object, _
    6.     ByVal e As System.EventArgs _
    7. ) Handles Button1.Click
    8.  
    9.     Dim frm2 As New Form2
    10.     frm2.frm1 = Me
    11.     frm2.ShowDialog()
    12.     frm2.Dispose()
    13. End Sub
    14.  
    15.  
    16. ' Form2 code
    17. Friend frm1 As Form1
    18.  
    19. Private Sub Form2_Load( _
    20.     ByVal sender As System.Object, _
    21.     ByVal e As System.EventArgs _
    22. ) Handles MyBase.Load
    23.  
    24.     MessageBox.Show(frm1.x)
    25. End Sub
    That's the loosest possible solution short of putting everything in a module. If the second form requires just one piece of data from the first form then it's a far better idea to pass just that piece of data rather than a form reference. Passing the data to a constructor is preferable if the second form MUST have that data. Otherwise a public property of the second form should be set from the first form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width