How To Pass Values From Form To Form In VB.Net

In this project you can pass variables and set other stuff from one form to another by settings there properties to equal another forms property. But for the most part this will help you understand how it can work for you. I’ve attached a small project for you to play with.

Create a new project, and create a new class file called PassThroughClass, and paste this code:

Public Class PassThroughClass
#Region "Variables"
 Public Shared eVar As New TextBox
 Public Shared fVar As String
 Public Shared intBool As Integer 'If 0, its not changed, if 1 its changed by form 1 Textbox1 KeyUp Event.
 'If 2 it's changed by frmMain Button1.Click Event. If its 3, its changed by frmMainButton3 Button Click event.
#End Region
#Region "Events"
 'Lets set the label text from this class...
 Public Shared Sub eVarChange(eString As String, intBoolValue As Integer)
 'Lets use this sub to change label on frmMain
 frmMain.Label1.Text = eString 'We have already passed the string to eString
 'from PassStuffToMe.Label1_Click event
 End Sub
 Public Shared Sub fVarChange(eString As String, intBoolValue As Integer)
 'Lets use this sub to change label on PassStuffToMe
 PassStuffToMe.Label1.Text = eString 'We have already passed the string to eString
 'from PassStuffToMe Button3_Click event.
 End Sub
 Public Shared Sub gVarChange(eString As String, intBoolValue As Integer)
 'Lets use this sub to change label on PassStuffToMe
 PassStuffToMe.Label1.Text = eString 'We have already passed the string to eString from Button4 on frmMain
 End Sub
#End Region
End Class

Next, you need a mew main form, so if you have a form already after creating this test project. Rename it frmMain and post this code into it:

Public Class frmMain
 Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp 'On Keyup Event
 If e.KeyCode = Keys.Enter Then 'Check if the key pressed on TextBox1 is the enter key
 PassThroughClass.intBool = 1 ' Set to 1 as per the comment legend we wrote beside the variable
 'On PassThroughClass class.
 PassStuffToMe.Label1.Text = TextBox1.Text 'We will call the class name of the other form first
 'Then we will call the label by name and use its .Text property to change the text value to equal
 'the TextBox1 on frmMain.
 End If
 End Sub
 Private Sub frmMain_Click(sender As Object, e As EventArgs) Handles MyBase.Click
 Dim fControl As Control
 For Each fControl In PassStuffToMe.Controls
 If (fControl.GetType() Is GetType(TextBox) Or (fControl.GetType() Is GetType(Label))) Then
 Dim eCtl As Control = CType(fControl, Control)
 eCtl.Text = "Reset by formMain"
 End If
 Next
 'We have reset all text properties on all controls.
 End Sub
 Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 PassThroughClass.eVar.Text = "My title was set by frmMain load event, and changed by button4 click event" 'Set the title of declared textbox on PassThroughClass
 PassStuffToMe.Show() 'Lets show the form PassStuffToMe so we can see the values changing on the form
 End Sub
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 PassThroughClass.intBool = 2 '2 is the value we used in the comments of the PassThroughClass
 'For the switch statement to set what is updating and how its been updated.
 PassThroughClass.eVarChange("Changed by PassThroughClass eVarChange Event", PassThroughClass.intBool)
 'This is how we pass values through a public class and a shared sub to change values on either form.
 End Sub
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 PassThroughClass.intBool = 2 'Lets set the switch statements variable
 PassStuffToMe.Label1.Text = Me.TextBox1.Text 'Me' refers to frmMain. Now we set the label1 on PassStuffToMe
 'to be whatever is in frmMain TextBox1.Text property.
 End Sub
 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
 PassThroughClass.intBool = 3 '3 is the value we used in the comments of the PassThroughClass
 'For the switch statement to set what is updating and how its been updated.
 PassThroughClass.fVarChange("Changed by PassThroughClass fVarChange Event", PassThroughClass.intBool)
 'This is how we pass values through a public class and a shared sub to change values on either form.
 End Sub
 Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
 PassThroughClass.intBool = 4
 PassThroughClass.gVarChange("The value of PassThroughClass.eVar TextBox was set by passing this string to it.", PassThroughClass.intBool)
 PassStuffToMe.Text = (PassThroughClass.eVar.Text)
 End Sub
End Class

Don’t mind the errors, we will get rid of them in a moment. Next step is to add a new form to your project and call it PassStuffToMe, and paste this code:

Public Class PassStuffToMe
 'With label1 we will use its text changed event to determine how label2 was changed.
 Private Sub Label1_TextChanged(sender As Object, e As EventArgs) Handles Label1.TextChanged
 Select Case PassThroughClass.intBool 'This is a switch statement.
 Case 0
 Return 'Do nothing if integer was not set in PassThroughClass.intBool to anything other than 0
 Case 1
 Label2.Text = "Label1 on PassStuffToMe Form, it was Changed by frmMain Textbox1 enter KeyUp Event"
 PassThroughClass.intBool = 0 'Lets reset our variable which allowed us into this case statement.
 Return
 Case 2
 Label2.Text = "Label1 on PassStuffToMe Form, it was Changed by frmMain Button2 Click Event"
 PassThroughClass.intBool = 0 'Lets reset our variable which allowed us into this case statement.
 Return
 Case 3
 Label2.Text = "Label1 on PassStuffToMe Form, it was Changed by PassThroughClass fVarChange Event"
 PassThroughClass.intBool = 0 'Lets reset our variable which allowed us into this case statement.
 Case 4
 Label2.Text = "The value of PassThroughClass.eVar TextBox was set by passing this string to it, by Button4 on frmMain."
 End Select
 End Sub
 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 frmMain.TextBox1.Text = "Button1 on PassStuffToMe Form, has changed frmMain TextBox1 Text from its Click Event"
 End Sub
 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
 PassThroughClass.fVarChange("Changed by PassThroughClasses fVarChange Event", PassThroughClass.intBool)
 End Sub
 Private Sub PassStuffToMe_Click(sender As Object, e As EventArgs) Handles MyBase.Click
 Dim fControl As Control
 For Each fControl In frmMain.Controls
 If (fControl.GetType() Is GetType(TextBox) Or (fControl.GetType() Is GetType(Label))) Then
 Dim eCtl As Control = CType(fControl, Control)
 eCtl.Text = "Reset by PassStuffToMe"
 End If
 Next
 'We have reset all text properties on all controls.
 End Sub
End Class

And now we will get rid of the errors on both forms by adding one TextBox, one Label, and four Buttons to frmMain.

Now for PassStuffToMe form, add two labels and two buttons. Now hit debug, and play around with it.

One other thing I forgot to mention, if you want to reset the labels, you don’t need to restart the debugger. Just click the form, and it will change all values of the textboxs and labels so you can continue clicking the buttons:

Dim fControl As Control 'Declare the control
 For Each fControl In PassStuffToMe.Controls 'Loop the controls of the form
 If (fControl.GetType() Is GetType(TextBox) Or (fControl.GetType() Is GetType(Label))) Then 'If the control is of type textbox or label, it will enter the if statement.
 Dim eCtl As Control = CType(fControl, Control)
 eCtl.Text = "Reset by formMain" 'Reset the text properties set by the buttons etc...
 End If
 Next
 'We have reset all text properties on all controls.

You should also put break points on all the buttons, and debug the code to see what its doing and how it works. Hope it helps – and if you want, you can purchase my example project below.