c# winform 子窗体向父窗体传值
父窗框mainForm;子窗体childForm,利用事件进行传值
在子窗体中的操作:
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);//当事件触发时,传递自身引用
}
}
在父窗体中的操作:
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;
}