If I have a value stored into a textbox of form1 and I have to pass that value into an another textbox of another form2. What is the method to do this passing values from one form to another?
-
possible duplicate of [How to update textbox in form1 from form2?](http://stackoverflow.com/questions/7969582/how-to-update-textbox-in-form1-from-form2) – Mark Hall Nov 27 '12 at 06:45
-
How is something like this considered a duplicate? The link above is a C# example not a VB example. – dragonore Feb 26 '15 at 05:35
6 Answers
There are a no. of ways.
1.Using TextChanged
event.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
- Using
Click
event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.TextBox1.Text = TextBox1.Text
End Sub
- Using
LostFocus
Event:
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
Form2.TextBox1.Text = TextBox1.Text
End Sub
Similarly you can work with every events.

- 584
- 2
- 13
- 23

- 460
- 1
- 6
- 15
In order to retrieve a control's value (e.g. TextBox.Text
) from another form, the best way is to create a module and create a property for the private variable.
An example of a property to hold a customer's first name:
Module modPrivateVariables
Private strCustomerFirstNameSTR As String
Public Property getCustomerFirstNameSTR() As String
Get
Return strCustomerFirstNameSTR
End Get
Set(ByVal strCustomerFirstName As String)
strCustomerFirstNameSTR = strCustomerFirstName
End Set
End Property
End Module
Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR
to hold the textbox's text. For example, if you had a textbox named (txtCustomerFirstName
), under its TextChanged event you would enter getCustomerFirstNameSTR = txtCustomerFirstName.Text
.
The textbox's text will now be assigned to getCustomerFirstNameSTR
property. Now you'll be able to access this property's value from anywhere and from any form in your application. For example if you had a textbox in another form say Form2 called "txtBoxInForm2" you can call txtBoxInForm2.Text = getCustomerFirstNameSTR
.
If you wanted to clear the value of the property then just type getCustomerFirstNameSTR = String.Empty
. The main thing to understand is that when you create a variable in one form (class) and try to access its value from another form (another class) then the variable has to be re-instantiated once.
This happens then the variable is reset to its default value which is an empty string. This will cause you to keep getting nothing (an empty textbox) every time you call it from another form. Properties don't need to be re-instantiated because they are accessed through public methods within the property itself (the get and set methods).

- 24,203
- 9
- 60
- 84

- 11
- 3
if both the forms are running, then you can use
form2.TextBox1.Text=form1.TextBox1.Text
Else you can declare a Public String variable in Form2, on any event,
dim Obj as new Form2
Obj.StrVariable=Me.TextBox1.Text
Obj.Show
and on Form2 Load,
Me.TextBox1.Text=StrVariable

- 728
- 11
- 23
In Form1.vb make sure you use an event such as Button.Click and inside that
Dim obb As New Form2
obb.val = Me.TextBox1.Text()
obb.Show()
Me.Hide()
In Form2.vb use a property called "val"
Public Property val As String
And on an event like MyBase.Load
TextBox1.Text = val

- 843
- 8
- 24
You could use the button1_click
and state there:
Dim obj as new form2
Obj.pass=me.textbox1.text
Obj.show()
Then in your form2 before your form2 main class you state:
Public property pass as string
On the load state
Textbox1.text=pass
Now, when you click on the button on form1, form2 will show and the textbox1 on form2 will have the same text as the one in form1. Provided you use this only with text box, labels or other kind of STRING or will work.

- 43
- 7
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' timer1 must be set to true
Timer1.Start() Form1.Show() Me.Hide()
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Form1.TextBox13.Text = TextBox1.Text

- 10,807
- 132
- 35
- 51