1.新建一个项目,分别添加MainForm、BaseChildForm两个窗体,其中MainForm的IsMdiContainer设为True。
2.在BaseChildForm里声明一个委托void ClosingHandler(),并重写OnFormClosing方法:
public delegate void ClosingHandler();
public ClosingHandler MyHandler;
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (MyHandler != null)
{
MyHandler();
}
base.OnFormClosing(e);
}
3.向MainForm里添加一个Panel,并定义一个方法void SeePanel():
private void SeePanel()
{
this.panel1.Visible = true;
}
4.添加一个用于创建子窗体对象和绑定其委托MyHandler的方法:
private void CreatChildForm(BaseChildForm childform)
{
childform.MdiParent = this;
childform.MyHandler += new BaseChildForm.ClosingHandler(this.SeePanel);
this.panel1.Visible = false;
childform.Show();
}
5.调用:创建一系列继承自BaseChildForm的子窗体,并在MainForm里的对应按钮的Click事件中调用CreatChildForm方法,如:
private void button1_Click(object sender, EventArgs e)
{
CreatChildForm(new ChildForm1());
}