3

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?

Bugs
  • 4,491
  • 9
  • 32
  • 41
user1460610
  • 31
  • 1
  • 2
  • 4
  • 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 Answers6

2

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
  1. Using Click event:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.TextBox1.Text = TextBox1.Text
    End Sub
  1. 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.

Spevacus
  • 584
  • 2
  • 13
  • 23
Kiran Rai Chamling
  • 460
  • 1
  • 6
  • 15
1

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).

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
0

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
Ashish Emmanuel
  • 728
  • 11
  • 23
0

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
zubair1024
  • 843
  • 8
  • 24
0

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.

-1
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
andrewsi
  • 10,807
  • 132
  • 35
  • 51