Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 从另一个窗体在文本框中设置值_Vb.net - Fatal编程技术网

Vb.net 从另一个窗体在文本框中设置值

Vb.net 从另一个窗体在文本框中设置值,vb.net,Vb.net,我正在使用vb.net 2013。我有3个表单:表单1、表单2、表单3 在表格1上我有一个按钮。按下此按钮时,form2打开。单击事件中的代码为: Dim dlg1 As New Form2 dlg1.Show(Me) Dim dlg2 As New Form3 dlg2.Show(Me) 在From2中,我有一个文本框(Txt1)和一个按钮。单击此按钮时,Form3将打开。单击事件中的代码为: Dim dlg1 As New Form2 dlg1.Show(Me) Dim dlg2

我正在使用vb.net 2013。我有3个表单:表单1、表单2、表单3

在表格1上我有一个按钮。按下此按钮时,form2打开。单击事件中的代码为:

Dim dlg1 As New Form2

dlg1.Show(Me)
Dim dlg2 As New Form3

dlg2.Show(Me)
在From2中,我有一个文本框(Txt1)和一个按钮。单击此按钮时,Form3将打开。单击事件中的代码为:

Dim dlg1 As New Form2

dlg1.Show(Me)
Dim dlg2 As New Form3

dlg2.Show(Me)
在form3中,我有一个按钮,用于在Form2的文本框(txt1)中设置值。我使用以下代码:

Form2.txt1.Text="123"
问题是我按下form3上的按钮后,form2上的文本框为空,没有设置任何值

我能做什么?(我不想改变表单打开的方式)


谢谢大家!

表单2.txt1
表单的引用

您正在此处使用一个新实例:

Dim dlg1 As New Form2
您需要替换以下代码:

Dim dlg1 As New Form2
dlg1.Show(Me)

或者将表单的
所有者
强制转换为上一个表单的类型,并设置属性(推荐):


只需使用Form2.Show()、Form3.Show()打开表单即可

然后,如果您将其放入Form3按钮中,请单击事件“Form2.txt1.Text=“123”,它将起作用。

您的Form2实例称为dlg2,因此以这种方式引用它(前提是该对话框仍然打开)。Form2只是引用了form classThe wonders of automatic instances…..dlg1很可能无法在dlg2的范围内访问。我尝试使用以下代码:CType(Me.Owner,Form2)。Txt1.Text=“123”但也有同样的问题。textbox上没有文本。@alex:请粘贴所有代码(或者创建一个空项目,尝试在那里复制它,并将其作为参考)。除非我们缺少一些细节,否则这种方法应该有效。但是,其他代码可能会产生干扰。没有其他代码。只有打开表单的代码和将值设置为textbox的代码。@alex:那你可以将项目作为ZIP文件共享吗?我可以看一下。我不知道为什么,但只有这样做:Dim fr as Form2=DirectCast(Me.Owner,Form2)fr.txt1.text=“123”