vb.net 从另一个表单在文本框中设置值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25097572/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:57:06  来源:igfitidea点击:

Setting a value in a textbox from another form

vb.net

提问by alex

I'm using vb.net 2013.I have 3 forms : Form1, Form2, Form3

我正在使用 vb.net 2013.I 有 3 个表单:Form1、Form2、Form3

On form1 I have a button . When this button is pressed , the form2 is open.The code inside the click event is :

在 form1 上,我有一个按钮。当这个按钮被按下时,form2 被打开。点击事件中的代码是:

Dim dlg1 As New Form2

dlg1.Show(Me)

Inside From2 I have a TextBox (Txt1) and a button .When this button is clicked the Form3 is open.The code inside the click event is :

在 From2 里面我有一个 TextBox (Txt1) 和一个按钮。当这个按钮被点击时,Form3 是打开的。点击事件中的代码是:

Dim dlg2 As New Form3

dlg2.Show(Me)

Inside form3 I have a button that I use to set a value in the textbox (txt1) on Form2. I use this code :

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

Form2.txt1.Text="123"

The problem is that after I press the button on form3 , the textbox on form2 is empty , no value is set.

问题是在我按下 form3 上的按钮后, form2 上的文本框为空,未设置任何值。

What can I do ? ( I don't want to change the way the forms are open)

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

Thank you !

谢谢 !

回答by Neolisk

Form2.txt1references default instanceof the form.

Form2.txt1引用表单的默认实例

You are using a new instance here:

您在这里使用了一个新实例:

Dim dlg1 As New Form2

You either need to replace this code:

您要么需要替换此代码:

Dim dlg1 As New Form2
dlg1.Show(Me)

With

Form2.Show(Me) 'not recommended

Or cast your form's Ownerto your previous form's type, and set the property (recommended):

或者将您的表单转换Owner为您之前表单的类型,并设置属性(推荐):

DirectCast(Me.Owner, Form2).txt1.Text = "123"

回答by Creator

Just open The forms with Form2.Show() , Form3.Show()
Then If you put this in your Form3 button click event " Form2.txt1.Text="123" " It will work.

只需使用 Form2.Show() , Form3.Show() 打开表单
然后如果你把它放在你的 Form3 按钮中,点击事件“ Form2.txt1.Text="123" ”它会起作用。