C#WinForm父級窗體內Panel容器中嵌入子窗體、程序主窗體設計例子
1 this.IsMdiContainer=true;//設置父窗體是容器
2 Son mySon=new Son();//實例化子窗體
3 mySon.MdiParent=this;//設置窗體的父子關係
4 mySon.Parent=pnl1;//設置子窗體的容器爲父窗體中的Panel
5 mySon.Show();//顯示子窗體,此句很重要,否則子窗體不會顯示
lock (this)
{
try
{
if (this.currentForm != null && this.currentForm == frm)
{
return;
}
else if (this.currentForm != null)
{
if (this.ActiveMdiChild != null)
{
this.ActiveMdiChild.Hide();
}
}
this.currentForm = frm;
frm.TopLevel = false;
frm.MdiParent = this;
panel.Controls.Clear();
panel.Controls.Add(frm);
frm.Show();
frm.Dock = System.Windows.Forms.DockStyle.Fill;
this.Refresh();
foreach (Control item in frm.Controls)
{
item.Focus();
break;
}
}
catch (System.Exception ex)
{
//
}
}