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