public void showMdiChild(System.Windows.Forms.Form MdiChildForm)
{
int i;
Form[] frmForms = this.MdiChildren;
bool btrue=false;
Cursor.Current=Cursors.WaitCursor;
for (i=0; i<frmForms.Length; i++)
{
//窗體類實例轉換成字符串時相同,說明是同一個實例
if (frmForms[i].ToString()==MdiChildForm.ToString())
{
MdiChildForm.Dispose(); //釋放剛創建的子窗體的實例資源
this.MdiChildren[i].Activate(); //將已經創建的實例窗體激活
this.MdiChildren[i].BringToFront ();
MdiChildForm.TopMost =true;
btrue=true;
}
}
// this.Controls
//如果i等於窗體數組的長度,
//說明上面的循環完成後沒有找到窗體實例與傳過來的窗體實例相同,
//說明新創建的窗體沒有被創建過,那麼就把它顯示出來
// frm.TopLevel=false;
if(btrue==false)
{
MdiChildForm.MdiParent = this; //指定窗體的MDI父窗體
MdiChildForm.Show();
if(MdiChildForm.MaximizeBox ==false)
{
MdiChildForm.WindowState = FormWindowState.Normal ;
MdiChildForm.StartPosition =FormStartPosition.CenterScreen;
}
else
MdiChildForm.WindowState = FormWindowState.Maximized ;
MdiChildForm.BringToFront ();
MdiChildForm.TopMost =true;
}
Cursor.Current =Cursors.Default ;
}
調用范例
frmSPZJ fm=new frmSPZJ();
showMdiChild(fm);