例如,两个窗体,一个主窗体mainForm,一个子窗体childForm。在mainForm中两个文本框,一个用于用户输入信息inputMainTextBox,一个用于显示子窗体显示信息childTextBox;在子窗体中也有两个文本框,一个用于显示主窗体用户输入信息的内容inputMainTextBox,另一个用于用户输入信息inputChildTextBox。
当单击主窗体按钮时,将主窗体中inputMainTextBox中的信息传递给子窗体可用如下:
1. 在子窗体中声明一个public 变量mainStr = null
2. 在主窗体button的单击事件中写如下:
childForm childform = new childForm();
childform.Owner = this;
childform.mainStr= this.inputMainTextBox.Text.Trim();
this.hide();
当单击子窗体中的按钮后,将inputChildTextBox中的信息传给主窗体中childTextBox,关闭子窗体,如下:
1. 在主窗体的资源文件中将childTextBox的类型由private改为public;
2. 在子窗体中声明一个主窗体类型的变量,如下:
public mainForm mainform = new mainForm();
3. 在单击主窗体按钮,打开子窗体时,写如下:
childForm childform = new childForm();
childform.Owner = this;
childform.mainform = this;
childform.Show();
this.hide();
4. 在单击子窗体按钮,打开主窗体时,写如下:
mainform.childTextBox.Text= inputChildTextBox.Text.Trim();
this.Close();
this.Owner.Show();
转自:http://hi.baidu.com/luqinhe/blog/item/699ee9fe1dc56e3b5d600832.html
相关资源:C# 两个窗体之间实时传递数据的实例