如何在Windows应用程序中从子窗体读取值到父窗体 [英] How to read values from child form to parent form in a windows application

查看:188
本文介绍了如何在Windows应用程序中从子窗体读取值到父窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个父窗体Form1,它在gridview中显示一个xml文件.当用户单击搜索按钮时,将显示Form2询问搜索关键字.根据给定的输入,我必须在Form1中显示结果.

在这里,我无法将搜索关键字从子窗体Form2转移到父窗体Form1.

在Form1中使用的代码Im:
Button1_click(对象发送者,EventArgs e)
{
Form2 f =新的Form2();
f.Show();
f.MdiParent = this;
//读取数据的代码

}


请向我提供此代码..

在此先感谢..

Hi,

I have a parent form Form1 which displays a xml file in the gridview. And when the user clicks the search button, Form2 will be displayed asking for the search keyword.Based on the input given, i have to display the result in Form1.

Here im not able to transfer search keyword from Child form Form2 to parent form Form1.

Code Im using in Form1:
Button1_click(Object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
f.MdiParent = this;
//code for reading data

}


Please provide me the code for this ..

Thanks in advance ..

推荐答案

您怎么认为这行得通?
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.


这篇关于如何在Windows应用程序中从子窗体读取值到父窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆