C#----如何让子窗体只显示一次

方法1:
下面代码是在主窗体的菜单项单击事件中编写
        private static DepartForm DF;   //DepartForm 为子窗体
        private void 部门管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DF == null || DF.IsDisposed)
            {
                DF = new DepartForm();
                DF.MdiParent = this;
                DF.Show();
            }
            else
            {
                DF.Activate();
            }
        }

方法2:
在父窗体中新建一个方法
   private void ShowChildForm(Form f, string formName)
        {
            System.Windows.Forms.Form[] mdiform = this.MdiChildren;
            bool openFlag = false;  //窗体的打开标志
            foreach (Form fr in mdiform)
            {
                if (fr.Name == formName)
                {
                    fr.Activate();
                    openFlag = true;
                    break;
                }
            }
            if (!openFlag)
            {
                f.MdiParent = this;
                //f.StartPosition = FormStartPosition.CenterParent;
                f.Show();
            }
        }
在菜单的单击事件中编写代码
    private void 部门管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DepartForm df = new DepartForm();
            ShowChildForm(df, df.Name.ToString());
        }

你可能感兴趣的:(学习总结)