写一个简单的委托与事件的示例,在winform开发中,经常遇到不同窗体调用事件,简单来说就是将方法作为参数进行传递,下面示例简单的阐述了如何使用;
窗体A调用窗体B
private void btn_impCase_Click(object sender, EventArgs e)
{
FormB f = new FormB();
f.initData += new **InitTree**(InitTreeData);
f.Show();
}
public void InitTreeData()
{
......
}
窗体B中定义委托与事件
**public delegate void InitTree(List<Common.CaseExpInfo> lt);
public event InitTree initData;**
private void btn_ok_Click(object sender, EventArgs e)
{
if(!string.IsNullOrEmpty( this.txt_path.Text))
{
** initData();**
}
this.Close();
}