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

这样就实现了。

原文地址:https://www.cnblogs.com/hfzsjz/p/4012892.html