子窗体 禁止子窗体被重复打开 (代码片段方法)

     /// <summary>
        /// 禁止子窗体被重复打开(打开窗体为默认大小)
        /// </summary>
        /// <param name="formChild">实例子窗体</param>
        public void FrmDisposeNormal(Form formChild)
        {

            bool isOpened = false;                      //定义子窗体打开标记,默认位false
            foreach (Form form in this.MdiChildren)     //循环MDI中的所有子窗体
            {
                //销毁其他不是要打开的窗口实例
                if (formChild.Name != form.Name)
                {
                    form.Dispose();
                }
                if (formChild.Name == form.Name)          //若该窗体已被打开
                {
                    formChild.Activate();               //激活该窗体
                    formChild.StartPosition = FormStartPosition.CenterParent;
                    formChild.WindowState = FormWindowState.Normal;
                    isOpened = true;                    //设置子窗体的打开标记为true
                    formChild.Dispose();                //销毁formChild实例
                    break;
                }
            }

            if (!isOpened)                              //若该窗体未打开,则显示该子窗体
            {
                formChild.MdiParent = this;
                formChild.Show();
            }

        }
        public void FrmDispose(Form formChild)
        {

            bool isOpened = false;                      //定义子窗体打开标记,默认位false
            foreach (Form form in this.MdiChildren)     //循环MDI中的所有子窗体
            {
                //销毁其他不是要打开的窗口实例
                if (formChild.Name != form.Name)
                {
                    form.Close();
                }                
            }
            if (!isOpened)                              //若该窗体未打开,则显示该子窗体
            {                
                formChild.Show();
            }

        }
        /// <summary>
        /// 禁止子窗体被重复打开(打开窗体为最大化)
        /// </summary>
        /// <param name="formChild">实例子窗体</param>
        public void FrmDisposeMax(Form formChild)
        {

            bool isOpened = false;                      //定义子窗体打开标记,默认位false
            foreach (Form form in this.MdiChildren)     //循环MDI中的所有子窗体
            {
                //销毁其他不是要打开的窗口实例
                if (formChild.Name != form.Name)
                {
                    // form.Dispose();
                    form.Close();
                }
                if (formChild.Name == form.Name)          //若该窗体已被打开
                {
                    formChild.Activate();               //激活该窗体
                    formChild.StartPosition = FormStartPosition.CenterParent;
                    formChild.WindowState = FormWindowState.Maximized;
                    isOpened = true;                    //设置子窗体的打开标记为true
                    formChild.Close();                //销毁formChild实例
                    break;
                }
            }

            if (!isOpened)                              //若该窗体未打开,则显示该子窗体
            {
                formChild.MdiParent = this;
                formChild.WindowState = FormWindowState.Maximized;
                formChild.Show();
            }
        }

 

子窗体 禁止子窗体被重复打开 (代码片段方法)

上一篇:将exe应用封装成windows服务


下一篇:CF219D Choosing Capital for Treeland (树形dp)