C# Winform 防止MDI子窗體重復打開


可以在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();
            }

這樣就實現了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM