简体   繁体   English

如何将文本框的值从一种形式传递到另一种形式

[英]How to pass value of a textbox from one form to another form

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.如果我将一个值存储到 form1 的文本框中,并且我必须将该值传递到另一个 form2 的另一个文本框中。 What is the method to do this passing values from one form to another?将值从一种形式传递到另一种形式的方法是什么?

There are a no.有一个没有。 of ways.的方式。

1.Using TextChanged event. 1.使用TextChanged事件。

    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:使用Click事件:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Form2.TextBox1.Text = TextBox1.Text
    End Sub
  1. Using LostFocus Event:使用LostFocus事件:
    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.同样,您可以处理每个事件。

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,否则,您可以在 Form2 中声明一个公共字符串变量,在任何事件中,

dim Obj as new Form2
Obj.StrVariable=Me.TextBox1.Text
Obj.Show

and on Form2 Load,并在 Form2 Load 上,

Me.TextBox1.Text=StrVariable

In Form1.vb make sure you use an event such as Button.Click and inside that在 Form1.vb 中确保你使用了一个事件,比如 Button.Click 和里面的

 Dim obb As New Form2
 obb.val = Me.TextBox1.Text()
 obb.Show()
 Me.Hide()

In Form2.vb use a property called "val"在 Form2.vb 中使用名为“val”的属性

Public Property val As String

And on an event like MyBase.Load在像 MyBase.Load 这样的事件上

TextBox1.Text = val

In order to retrieve a control's value (eg TextBox.Text ) from another form, the best way is to create a module and create a property for the private variable.为了从另一个表单检索控件的值(例如TextBox.Text ),最好的方法是创建一个模块并为私有变量创建一个属性。

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.然后在文本框的 TextChanged 事件中使用属性getCustomerFirstNameSTR来保存文本框的文本。 For example, if you had a textbox named ( txtCustomerFirstName ), under its TextChanged event you would enter getCustomerFirstNameSTR = txtCustomerFirstName.Text .例如,如果您有一个名为 ( txtCustomerFirstName ) 的文本框,则在其 TextChanged 事件下,您将输入getCustomerFirstNameSTR = txtCustomerFirstName.Text

The textbox's text will now be assigned to getCustomerFirstNameSTR property.文本框的文本现在将分配给getCustomerFirstNameSTR属性。 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 .例如,如果您有一个名为“txtBoxInForm2”的 Form2 形式的文本框,您可以调用txtBoxInForm2.Text = getCustomerFirstNameSTR

If you wanted to clear the value of the property then just type getCustomerFirstNameSTR = String.Empty .如果您想清除该属性的值,则只需键入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).属性不需要重新实例化,因为它们是通过属性本身内的公共方法(get 和 set 方法)访问的。

You could use the button1_click and state there:您可以使用button1_click并在那里声明:

Dim obj as new form2
Obj.pass=me.textbox1.text

Obj.show()

Then in your form2 before your form2 main class you state:然后在form2主类之前的form2中声明:

Public property pass as string 

On the load state在负载状态

Textbox1.text=pass 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.现在,当您单击 form1 上的按钮时,将显示 form2,并且 form2 上的 textbox1 将具有与 form1 中相同的文本。 Provided you use this only with text box, labels or other kind of STRING or will work.前提是您仅将其与文本框、标签或其他类型的 STRING 一起使用,否则将起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用VB.Net中的继承将文本框的值从一种形式获取到另一种形式 - How to get value of textbox from one form to another form using inheritance in VB.Net 如何将值从一个表单传递到另一个表单的动态创建控件 - How to pass value from one form to another form's dynamically created control 如何将 label 的值从一种形式传递到 VB.NET 中的另一种形式? - How to pass value of a label from one form to another form in VB.NET? 将文本框信息从一种形式传递到另一种形式 - Passing Textbox information from one form to another 在VB.NET中将整数值从一种形式传递到另一种形式 - Pass integer value from one form to another in VB.NET 在另一种形式的文本框中设置值 - Setting a value in a textbox from another form 如何将文本从一个文本框保存/追加到VB中同一表单上的另一个文本框 - How can I save/append text from one textbox to another textbox on the same Form in VB 如何将Public变量从一个表单传递到另一个表单 - How Do i pass a Public variable from one form to another 将文本框上的值转移到其他窗体上的另一个文本框 - Transfer value on textbox to another textbox on a different form 如何将值从模态弹出式扩展器内的gridview传递到父窗体的文本框? - How to pass value from gridview inside modal pop up extender to textbox in parent form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM