0

我正在使用带有 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 时,它会一次又一次地打开新表单。这意味着选项卡控件中的表单重复

4

3 回答 3

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();
    }
于 2014-09-19T20:24:05.330 回答
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;
   }
于 2013-06-20T11:21:52.947 回答
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;

        }
    }
于 2018-12-12T14:11:21.357 回答