How to pass value of a textbox from one form to another form

前端 未结 6 1649
广开言路
广开言路 2021-01-21 09:06

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 fo

相关标签:
6条回答
  • 2021-01-21 09:23

    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.

    0 讨论(0)
  • 2021-01-21 09:27

    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
    
    0 讨论(0)
  • 2021-01-21 09:28
    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
    
    0 讨论(0)
  • 2021-01-21 09:37

    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
    
    0 讨论(0)
  • 2021-01-21 09:39

    In order to retrieve a control's value (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 text box text changed event use the property(getCustomerFirstNameSTR) To hold the text box's text. For example, if you had a text box named (txtCustomerFirstName) UNder it's text changed event you would enter getCustomerFirstNameSTR = txtCustomerFirstName.Text.

    The text box'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 text box 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 text box) every time you call it from another form. Properties don't need to be re-instantiated because they are accessed through public methods with in the property it self (the get and set) methods.

    0 讨论(0)
  • 2021-01-21 09:40

    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.

    0 讨论(0)
提交回复
热议问题