可以在MDI主窗體中添加以下方法。
//防止打開多個窗體 private bool ShowChildrenForm(string p_ChildrenFormText) { int i; //依次檢測當前窗體的子窗體 for (i = 0; i < this.MdiChildren.Length; i++) { //判斷當前子窗體的Text屬性值是否與傳入的字符串值相同 if (this.MdiChildren[i].Name == p_ChildrenFormText) { //如果值相同則表示此子窗體為想要調用的子窗體,激活此子窗體並返回true值 this.MdiChildren[i].Activate(); return true; } } //如果沒有相同的值則表示要調用的子窗體還沒有被打開,返回false值 return false; }
調用窗體打開代碼如下:
if (!ShowChildrenForm("F_Dwxx")) { F_Dwxx f = new F_Dwxx(); f.MdiParent = this; f.Show(); }
這樣就實現了。