如何避免子MDI选项卡控件中的重复选项卡

时间:2013-06-20 11:16:35

标签: c# winforms window

我正在使用带有c#winforms的.NET 3.5。在这我正在使用MDI子选项卡控件。如果我打开表单它会正常工作,它会成功打开。如果我再次打开相同的表格,它会打开。这意味着重复选项卡。

我的代码如下......

private void Main_MdiChildActivate(object sender, EventArgs e)
        {
            if (this.ActiveMdiChild == null)
                tabForms.Visible = false; // If no any child form, hide tabControl
            else
            {
                this.ActiveMdiChild.WindowState = FormWindowState.Maximized; // Child form always maximized

                if (this.ActiveMdiChild.Tag == null)
                {
                    TabPage tp = new TabPage(this.ActiveMdiChild.Text);
                    tp.Tag = this.ActiveMdiChild;
                    tp.Parent = tabForms;
                    tabForms.SelectedTab = tp;

                    this.ActiveMdiChild.Tag = tp;
                    this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(ActiveMdiChild_FormClosed);
                }

                if (!tabForms.Visible) tabForms.Visible = true;
            }
        }

在此,每次this.ActiveMdiChild.Tag取值为null,因此它会一次又一次地打开新表单。这意味着在选项卡控件中重复表单

3 个答案:

答案 0 :(得分:0)

添加上述方法,以检查名称中的表单是否为mdi父级中的子项。

  public static bool FormExist(string formName, out Form frm)
    {
        frm = null;
        bool exist = false;

        Form[] f = yourMdiParent.ActiveForm.MdiChildren;
        foreach (Form ff in f)
        {
            if (ff.Name == formName)
            {
                frm = ff;
                exist = true;
                break;
            }
        }

        return exist;
    }

并添加关于添加子表单的检查。

   Form forma;
   if(FormExist("yourchildformid",out forma) && forma !=null)
   {
      forma.Focus();
      return;
   }

答案 1 :(得分:0)

我迟到了,但我用了:

    private void serviceManagerToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // prevent duplicates
        if (Application.OpenForms.OfType<ServiceManager>().FirstOrDefault() != null)
        {
            return;
        }
        ServiceManager serviceManager = new ServiceManager { MdiParent = this, WindowState = FormWindowState.Maximized };
        serviceManager.Show();
    }

答案 2 :(得分:0)

通过结合其他解决方案,我能够在短短的几个星期内使它正常工作

将此功能添加到您的父MDI表单

    private bool checkTabExists(string tabVal)
    {
        foreach (TabPage tab in tabForms.TabPages)
        {
            if (tab.Text == tabVal)
                return true;
        }
        return false;
    }

然后修改原始的Form_MdiChildActivate以包括其他检查

    private void Main_MdiChildActivate(object sender, EventArgs e)
    {
        if (this.ActiveMdiChild == null)
            tabForms.Visible = false; // If no any child form, hide tabControl
        else
        {
            this.ActiveMdiChild.WindowState = FormWindowState.Maximized; // Child form always maximized

            if(checkTabExists(this.ActiveMdiChild.Name))
            {
                //If the Child Form already Exists Go to it
                foreach (TabPage tab in tabForms.TabPages)
                {
                    if (tab.Text == this.ActiveMdiChild.Name)
                        tabForms.SelectedTab = tab;
                }

            }
            // If child form is new and has no tabPage, create new tabPage
            else if (this.ActiveMdiChild.Tag == null)
            {
                ActiveMdiChild.TopLevel = false;
                ActiveMdiChild.Dock = DockStyle.Fill;
                ActiveMdiChild.FormBorderStyle = FormBorderStyle.None;

                // Add a tabPage to tabControl with child form caption
                TabPage tp = new TabPage(this.ActiveMdiChild.Text);
                tp.Tag = this.ActiveMdiChild;
                tp.Parent = tabForms;
                tabForms.SelectedTab = tp;

                this.ActiveMdiChild.Tag = tp;
                this.ActiveMdiChild.FormClosed += new FormClosedEventHandler(Main_FormClosed);
            }

            if (!tabForms.Visible) tabForms.Visible = true;
            //tabForms.AutoSize = true;
            //tabForms.TabPages[0].Height = 38;

        }
    }