【问题标题】:How to Avoid Duplicate tabs in child MDI Tab Control如何避免子 MDI 选项卡控件中的重复选项卡
【发布时间】:2013-06-20 11:16:34
【问题描述】:

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

【问题讨论】:

  • 选项卡式窗口模型与 MDI 相比,就像水与火相比。使用其中之一,而不是两者。

标签: c# winforms window


【解决方案1】:

添加上述方法以检查具有名称的表单是否在 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;
   }

【讨论】:

  • 我不确定这个方法是否可以是静态的,因为您需要访问实例变量?另外,为什么不返回 Form,如果找不到则返回 null,如果找到则返回实际的 Form。
  • 在我使用它的类中,我只有静态。你可以改变它。如果返回 null 则初始化它并将其添加到 mdi 父级。这段代码在我的 erp 中运行多年。 :)
【解决方案2】:

我迟到了,但我使用:

    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();
    }

【讨论】:

    【解决方案3】:

    通过结合其他解决方案,我能够在几周内完成这项工作

    将此功能添加到您的父 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;
    
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多