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 variableaccessbetweenforms (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:
Public Class Form1
Friend text1 As String = ""
Friend text2 As String = ""
Friend text3 As String = ""
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Form2.Visible = True
Form2.Focus()
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox1.TextChanged
text1 = TextBox1.Text
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox2.TextChanged
text2 = TextBox2.Text
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TextBox3.TextChanged
text3 = TextBox3.Text
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Size = New Size(268, 20)
TextBox2.Size = New Size(268, 20)
TextBox3.Size = New Size(268, 20)
Button1.Size = New Size(80, 25)
TextBox1.Location = New Point(12, 12)
TextBox2.Location = New Point(12, 38)
TextBox3.Location = New Point(12, 64)
Button1.Location = New Point(200, 228)
Button1.Text = "Show Form 2!"
End Sub
End Class
Copy this into Form2's code segment:
VB Code:
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Me.Visible = False
Form1.Focus()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = Form1.text1
TextBox2.Text = Form1.text2
TextBox3.Text = Form1.text3
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.ReadOnly = True
TextBox2.ReadOnly = True
TextBox3.ReadOnly = True
TextBox1.Size = New Size(268, 20)
TextBox2.Size = New Size(268, 20)
TextBox3.Size = New Size(268, 20)
Button1.Size = New Size(80, 25)
Button2.Size = New Size(80, 25)
TextBox1.Location = New Point(12, 12)
TextBox2.Location = New Point(12, 38)
TextBox3.Location = New Point(12, 64)
Button1.Location = New Point(200, 228)
Button2.Location = New Point(200, 90)
Button1.Text = "Hide Form 2!"
Button2.Text = "Copy Text!"
End Sub
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.
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.