子窗口调用父窗口委托事件

父窗口

namespace WinformTransferValue

{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
//注册事件
frm.TransfEvent += frm_TransfEvent;
frm.ShowDialog();
}
//事件处理方法
void frm_TransfEvent(string value)
{
textBox1.Text = value;
}
}
}

子窗口

namespace WinformTransferValue
{
//声明委托 和 事件
public delegate void TransfDelegate(String value);
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

public event TransfDelegate TransfEvent;
private void button1_Click(object sender, EventArgs e)
{
//触发事件

if(TransfEvent!=null){
  TransfEvent(textBox1.Text);

}

this.Close();
}
}
}

posted @ 2016-08-23 19:24  xszjk  阅读(332)  评论(0)    收藏  举报