父窗框mainForm;子窗体childForm,利用事件进行传值
在子窗体中的操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public event EventHandler accept;
public string value;
private void btnStart_Click( object sender, EventArgs e)
{
value=txtName.text; if (accept!= null )
{ accept( this , EventArgs.Empty); //当事件触发时,传递自身引用
} }
|
在父窗体中的操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
childForm frmChild= new childForm();
private void btnForm_Click( object sender, EventArgs e)
{
if (frmChild.IsDisposed)
{ frmChild= new childForm(); //时刻保持只有一个窗体显示
} frmChild.accept += new EventHandler(Main_accept);
frmChild.Show();
} //父窗体处理子窗体传来的值 public void Main_accept( object sender, EventArgs e)
{ childForm frmChild= (childForm)sender;
string childValue = childForm .value;
txtUser.text=childValue; }
|
本文转自静默虚空博客园博客,原文链接:http://www.cnblogs.com/jingmoxukong/articles/2118101.html,如需转载请自行联系原作者