三个form设置父子窗口的问题
现有窗口:form1、form2、form3
想要在form2的Load()事件中将form3设置为from1的子窗口
请教会的老师。
form2_Load()
{
form2.IsMdiContainer = true;
Form3 wnd = new Form3();
wnd.MdiParent = ???
wnd.Show();
}
------解决方案--------------------上面的方法是在Form1里面完成Form2的初始化, 如果楼主不想这样, 可以就要用到静态变量了, 又或者用Singleton Pattern
e.g.
public partial class Form1: Form
{
private static volatile Form1 _instance;
private static object syncRoot=new object();
public Form1()
{
//to initialize Form1 here...
}
public static Form1 Instance
{
get
{
if(_instance == null)
{
lock(syncRoot)
{
if(_instanc == null)
{
_instance = new Form1();
}
}
}
return _instance;
}
}
}
//Then, in your Form2, you can...
form2_Load()
{
form2.IsMdiContainer = true;
Form3 wnd = new Form3();
wnd.MdiParent = Form1.Instance;//=========> See?
wnd.Show();
}