[英]vb.net Pass Textbox Between forms.
I have 2 forms. 我有两种形式。 On Form1 I want to pass the textbox value to form 2 on load. 在Form1上,我想在加载时将文本框值传递给Form 2。 This is what I thought would work. 我认为这是可行的。 Form1 will be loaded and running and data will be populated in form1. 将加载并运行Form1,并在form1中填充数据。 I am exposing the property of the text box in form1. 我在form1中公开了文本框的属性。 Then I am trying to use that exposed property in form2. 然后,我尝试在form2中使用该公开属性。
Public Class form1
Public ReadOnly Property GetTextBox() As String
Get
Return txtbox1.Value
End Get
End Property
On form2 在form2上
Dim X As form1
Me.TextBox1.Text = X.GetTextBox
VB will allow you to refer to instances of forms by their class name , so you'd be able to use: VB允许您通过类的类名来引用表单实例 ,因此您可以使用:
Me.TextBox1.Text = Form1.GetTextBox
However you should not rely on this and should instead pass an explicit instance of Form1
to Form2
, eg in a constructor: 但是,您不应该依赖于此,而应该将Form1
的显式实例传递给Form2
,例如在构造函数中:
' Form2
Public Sub New(ByVal f As Form1)
Me.New()
' save 'f' for future reference
End Sub
There are a handful of ways to skin this cat. 有几种方法可以给这只猫蒙皮。
The simplest would be to create a second (or replace the existing) constructor for Form2
that accepts a string
as an parameter. 最简单的方法是为Form2
创建第二个(或替换现有的)构造函数,该构造函数接受string
作为参数。 Then when Form1
creates Form2
you can pass the argument that way. 然后,当Form1
创建Form2
您可以以这种方式传递参数。
Public Class Form2
Sub New(ByVal txt As String)
InitializeComponent()
Me.TextBox1.Text = txt
End Sub
End Class
Then in Form1
you'd have something like Dim f2 As Form2 = New Form2(myTextBox.Text)
然后在Form1
您将得到类似Dim f2 As Form2 = New Form2(myTextBox.Text)
The other ways are honestly basically the same as this, except you could pass the Textbox
itself as an argument to the constructor, or even Form1
and assign Dim X As Form1 = theForm
in the constructor. 坦白地说,其他方式基本上与此相同,除了您可以将Textbox
本身作为参数传递给构造函数,甚至可以将Form1
传递给Dim X As Form1 = theForm
构造函数。 Generally speaking, if you don't need anything more than just Textbox.Text
then you should only accept a string
in the constructor. 一般而言,如果您仅需要Textbox.Text
那么您仅应在构造函数中接受string
。 No reason to expose an entire control or form if you don't need all of it! 如果您不需要全部控件或表单,则没有理由公开!
Your current code is pretty close, but as Plutonix commented your Form2
's X
property is just another instance of Form1
, not the actual instance that's being displayed by the application. 您当前的代码非常接近,但是正如Plutonix所评论的那样, Form2
的X
属性只是Form1
另一个实例,而不是应用程序正在显示的实际实例。
When you do this: 执行此操作时:
Dim X As form1
You're create a new reference to a form1
. 您将创建对form1
的新引用。 (And presumably instantiating it somewhere? Or perhaps relying on a feature in VB whereby a "default" form instance is used . Which... don't do that. Just trust me, don't.) This instance is entirely unrelated to the instance that already exists. (大概在某个地方实例化它?或者可能依赖于VB中使用“默认”表单实例的功能 。...不这样做。请相信我,不要。)此实例与已经存在的实例。 What you're looking for is a reference to the instance that already exists. 您正在寻找的是对已经存在的实例的引用。
If form2
has a dependency on form1
and requires a reference to an instance of form1
then require that reference on the constructor for form2
: 如果form2
有一个依赖form1
,需要对实例的引用form1
则需要在构造为参考form2
:
Private Property Form1Instance As form1
Sub New(ByVal form1Instance As form1)
Me.Form1Instance = form1Instance
End Sub
When you create your instance of form2
, provide it with a reference to the form1
instance: 创建form2
实例时,请提供对form1
实例的引用:
Dim form2Instance As New form2(Me)
form2Instance.Show()
Then within form2
you can reference that existing instance of form1
: 然后在form2
您可以引用该form1
现有实例:
Dim someVariable As String = Me.Form1Instance.GetTextBox()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.