用委托和事件来实现
首先在主窗口声明一个公共委托,假设主窗口叫MainForm
public partial class MianForm : Form
{
public delegate void OpenFormDelegate(string formname);
}
在子窗口UsersList声明事件
public partial classUsersList : Form
{
public event MianForm.OpenFormDelegate openformevent;
}
Main打开UsersList时实例化该事件
public partial class MianForm : Form
{
public delegate void OpenFormDelegate(string formname);
private void button_click()//假设主窗口单击某按钮打开UsersList
{
UsersList childForm = new UsersList();
UsersList.openformevent+=new OpenFormDelegate(delegateopenform);//
childForm.Show(dockPanel1);
}
private void delegateopenform(strong formname)//OpenFormDelegate要调用的方法
{
switch(formname)
{
case "UsersList2"://打开UsersList2,假设UsersList2存在
UsersList2 childForm2 = new UsersList();
childForm2.Show(dockPanel1);
break;
case "UsersList3":
break;
case "UsersList4":
break;
.
.
default:
break;
}
}
}
UsersList下触发事件
public partial classUsersList : Form
{
public event MianForm.OpenFormDelegate openformevent;
private void button_click()//假设点击某按钮
{
if(openformevent!=null)//如果事件注册
{
openformevent("UsersList2");//委托主窗口打开名为UsersList2的窗口
}
}
}
主窗口与子窗口好比老板和员工,老板可以加人到公司,员工不用,但是员工可以介绍同学给老板,老板来处理