vb.net 如何将文本框的值从一种形式传递到另一种形式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13578670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass value of a textbox from one form to another form
提问by user1460610
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?
如果我将一个值存储到 form1 的文本框中,并且我必须将该值传递到另一个 form2 的另一个文本框中。将值从一种形式传递到另一种形式的方法是什么?
回答by Kiran Rai Chamling
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
- Using
Click
event:
- 使用
Click
事件:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.TextBox1.Text = TextBox1.Text
End Sub
- 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.
同样,您可以处理每个事件。
回答by Esteban Luques
You could use the button1_click
and state there:
你可以在button1_click
那里使用and 状态:
Dim obj as new form2
Obj.pass=me.textbox1.text
Obj.show()
Then in your form2 beforeyour 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. Provided you use this only with text box, labels or other kind of STRING or will work.
现在,当您单击 form1 上的按钮时,将显示 form2,并且 form2 上的 textbox1 将具有与 form1 中相同的文本。前提是您仅将其与文本框、标签或其他类型的 STRING 一起使用,否则将起作用。
回答by Robert Cotton
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.
为了从另一个表单中检索控件的值 (TextBox.Text)。最好的方法是创建一个模块并为私有变量创建一个属性。保存客户名字的属性示例。
Module modPrivateVariables
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
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
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.
然后在文本框文本更改事件中使用属性(getCustomerFirstNameSTR) 来保存文本框的文本。例如,如果您有一个名为 (txtCustomerFirstName) 的文本框,在它的文本更改事件下,您将输入 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.
文本框的文本现在将分配给“getCustomerFirstNameSTR”属性。现在,您将能够从应用程序中的任何位置和任何表单访问此属性的值。例如,如果您有一个名为“txtBoxInForm2”的 Form2 形式的文本框,您可以调用 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.
如果您想清除该属性的值,只需键入 getCustomerFirstNameSTR = String.Empty。要理解的主要事情是,当您以一种形式(类)创建变量并尝试从另一种形式(另一个类)访问其值时,必须重新实例化该变量一次。
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.
发生这种情况后,变量将重置为其默认值,即空字符串。这将导致您每次从另一个表单调用它时都没有得到任何东西(一个空的文本框)。属性不需要重新实例化,因为它们是通过公共方法访问的,属性本身(get 和 set)方法。
回答by Ashish Emmanuel
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
回答by zubair1024
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
回答by krismorgan
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