Results 1 to 1 of 1

Thread: VB.Net - Accessing/Using variables on multiple forms

  1. #1

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    VB.Net - Accessing/Using variables on multiple forms

    A common problem when developing an application that is not contained within one form is accessing variables of one form from within another. Here, I will show you one implementation that copies the text from three textboxes from one form to another.

    As with any problem, there are many solutions to accessing variables between forms. Though I had trouble finding the answer to my question originally, when preparing for this article, I came accross a number of resources regarding variable access between forms. I would like to acknowledge them now and I suggest you look at them all (at least briefly), as they all have different slants on solving this problem, most of which I will not cover here (I could write a book on the different techniques of solving just this problem if I went into enough detail).

    First, jmcilhinney has a number of links regarding the issue of variable access between forms (and many other issues) in his signature. Second, there was a previous code bank submission regarding this issue that briefly covered solutions, but I felt a little more detail would be helpful (and the title doesn't really make it obvious what it is about). Third, there's a thread that talked about this issue a little.

    All of those give code snippets to point you in the right direction, but I felt it was important enough to give a whole program (ok, so I wrote the program first then thought "hey this'd go well on the code bank", but who'll ever know, right?).

    To run this program, create a new VB project, add 3 textboxes and 1 button to form1, add another form to the project, and add 3 textboxes and 2 buttons to this form2. I stuck with all the default names so that it would be easy to copy. Finally copy the code below over the top of the original form's code (double click the form to access it quickly for anyone who's asking), then compile the program and test it out.

    Copy this into Form1's code segment:
    VB Code:
    1. Public Class Form1
    2.  
    3.     Friend text1 As String = ""
    4.     Friend text2 As String = ""
    5.     Friend text3 As String = ""
    6.  
    7.     Private Sub Button1_Click(ByVal sender As System.Object, _
    8. ByVal e As System.EventArgs) Handles Button1.Click
    9.         Form2.Visible = True
    10.         Form2.Focus()
    11.     End Sub
    12.  
    13.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
    14. ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    15.         text1 = TextBox1.Text
    16.     End Sub
    17.  
    18.     Private Sub TextBox2_TextChanged(ByVal sender As System.Object, _
    19. ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    20.         text2 = TextBox2.Text
    21.     End Sub
    22.  
    23.     Private Sub TextBox3_TextChanged(ByVal sender As System.Object, _
    24. ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    25.         text3 = TextBox3.Text
    26.     End Sub
    27.  
    28.     Private Sub Form1_Load(ByVal sender As System.Object, _
    29. ByVal e As System.EventArgs) Handles MyBase.Load
    30.         TextBox1.Size = New Size(268, 20)
    31.         TextBox2.Size = New Size(268, 20)
    32.         TextBox3.Size = New Size(268, 20)
    33.         Button1.Size = New Size(80, 25)
    34.         TextBox1.Location = New Point(12, 12)
    35.         TextBox2.Location = New Point(12, 38)
    36.         TextBox3.Location = New Point(12, 64)
    37.         Button1.Location = New Point(200, 228)
    38.         Button1.Text = "Show Form 2!"
    39.     End Sub
    40. End Class

    Copy this into Form2's code segment:
    VB Code:
    1. Public Class Form2
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, _
    4. ByVal e As System.EventArgs) Handles Button1.Click
    5.         Me.Visible = False
    6.         Form1.Focus()
    7.     End Sub
    8.  
    9.     Private Sub Button2_Click(ByVal sender As System.Object, _
    10. ByVal e As System.EventArgs) Handles Button2.Click
    11.         TextBox1.Text = Form1.text1
    12.         TextBox2.Text = Form1.text2
    13.         TextBox3.Text = Form1.text3
    14.     End Sub
    15.  
    16.     Private Sub Form2_Load(ByVal sender As System.Object, _
    17. ByVal e As System.EventArgs) Handles MyBase.Load
    18.         TextBox1.ReadOnly = True
    19.         TextBox2.ReadOnly = True
    20.         TextBox3.ReadOnly = True
    21.         TextBox1.Size = New Size(268, 20)
    22.         TextBox2.Size = New Size(268, 20)
    23.         TextBox3.Size = New Size(268, 20)
    24.         Button1.Size = New Size(80, 25)
    25.         Button2.Size = New Size(80, 25)
    26.         TextBox1.Location = New Point(12, 12)
    27.         TextBox2.Location = New Point(12, 38)
    28.         TextBox3.Location = New Point(12, 64)
    29.         Button1.Location = New Point(200, 228)
    30.         Button2.Location = New Point(200, 90)
    31.         Button1.Text = "Hide Form 2!"
    32.         Button2.Text = "Copy Text!"
    33.     End Sub
    34. End Class

    Now test the program out by typing in the first form's textboxes, showing the second form, and clicking the "Copy Text!" button. Enjoy!

    P.S. note that this is only one solution using friend variables. There are many others (eg creating accessor functions), but I used this to understand the concepts. If anybody has a solution using modules (or other methods), feel free to add it to this thread.
    Attached Images Attached Images  
    Last edited by ZaNi; Jun 27th, 2006 at 07:51 PM.
    * Don't limit yourself to sanity
    * I'd rather be optimistic and naive than pessimistic and right
    * Ask good questions, get good answers.

    My Codebank submissions:
    How to write one rountine to handle many events
    Accessing and Using variables across multiple forms
    Printing/Previewing a form or control

    Links I've "borrowed" from other people:
    vbdotnetboy - Awesome site for tips

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