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

前端 未结 6 1651
广开言路
广开言路 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: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.

提交回复
热议问题