且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Windows应用程序中从子窗体读取值到父窗体

更新时间:2023-12-06 10:02:40

您怎么认为这行得通?
How so you suppose that is going to work?
Form2 f = new Form2();

创建表单的新实例,到目前为止还不错.

Creates a new instance of your form, fine so far.

f.Show();

开始显示表单,并立即返回控件.

Starts to display the form, and returns control immediately.

f.MdiParent = this;

设置刚刚显示的表单的父级...

Sets the parent of the form you have just shown...

//code for reading data

什么数据?当您不看时,用户输入的速度真的很快吗?

问题:
1)为什么Form2是MdiChild?需要吗?还是普通的消息框类型的表格是更好的选择?
2)无论如何,您认为用户输入的速度有多快?

更好的方法(但不是所有细节,因此您必须做一些t =您的家庭作业...):

What data? Did the user type really fast when you weren''t looking?

Questions:
1) Why is Form2 a MdiChild? Does it need to be? Or would a normal message box type form be a better option?
2) How fast do you think the user is going to type, anyway?

Better way (but not all the details, so you have to do some work on t=your homework...):

frmGetKeyword fgk = new frmGetKeyword();
if (fgk.ShowDialog() == DialogResult.OK)
   {
   string searchKeyword = fgk.Text;
   ...
   }

请注意,我已将您的Form2的名称更改为描述性名称.将任何东西保留为VS的默认值就像拥有一个新的乐队,称为Band1,Band2等.很难记住哪个乐曲可以播放您喜欢的音乐...
使用ShowDialog而不是Show可以确保用户在控件返回到您的方法之前输入其数据.
现在,您要做的就是以新形式提供一个公共的"Text"属性,该属性返回搜索关键字.

Note that I have changed the name of your Form2 to something descriptive. Leaving anything at the VS default is like having each new band that forms called Band1, Band2, etc. A little difficult to remember which plays music you like...
Using ShowDialog rather than Show ensures that the user enters his data before control returns to your method.
Now all you have to do is provide a public "Text" property in the new form, which returns the search keyword.