选择按钮单击打开的选项卡

时间:2012-03-14 03:10:11

标签: c# winforms tabs

我有一个带有按钮的表单。如果我单击按钮,则会打开另一个表单。如果我在tab的帮助下返回到父窗体并再次单击相同的按钮,它什么都不做。

这是我的代码:

private void pictureBox1_Click(object sender, EventArgs e)
        {
             form wadd = new form(this);
             if ((IsFormAlreadyOpen(typeof(form))) == null)
             {
                 wadd.MdiParent = Form1.ActiveForm;
                 wadd.Show();
             }          
        }
 public static Form IsFormAlreadyOpen(Type FormType)
        {
            foreach (Form OpenForm in Application.OpenForms)
            {
                if (OpenForm.GetType() == FormType)
                    return OpenForm;
            }
            return null;
        }  



private void Form1_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 child form is new and no has tabPage, 
                // create new tabPage 
                if (this.ActiveMdiChild.Tag == null)
                {
                    // 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(ActiveMdiChild_FormClosed);
                }
                else
                {

                    tab();
                }

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

            }
        }
        public void tab()
        {
            for (int i = 0; i < tabForms.TabCount; i++)
            {
                if (tabForms.TabPages[i].Text == this.ActiveMdiChild.Text.ToString())
                {
                    tabForms.SelectedTab = tabForms.TabPages[i];
                    break;
                }
            }
        }

        private void ActiveMdiChild_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Destroy the corresponding Tabpage when closing MDI child form
            if (tabForms.HasChildren)
            {
                ((sender as Form).Tag as TabPage).Dispose();
            }            
            //If no Tabpage left
            else if (!tabForms.HasChildren)
            {
                tabForms.Visible = false;                
            }            
        }

        private void tabForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((tabForms.SelectedTab != null) && (tabForms.SelectedTab.Tag != null))
                (tabForms.SelectedTab.Tag as Form).Select();            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {            
            if (!tabForms.HasChildren)
            {
                Application.Exit();
            }            
        }

我想要的结果是当我再次点击该按钮时,标签应该集中在这个表格而不是打开不允许的新表格。

2 个答案:

答案 0 :(得分:0)

你想要这样的东西我相信:

form wadd;
private void pictureBox1_Click(object sender, EventArgs e)
    {
         if (wadd == null)
         {
             wadd = new form(this);
         }
         wadd.MdiParent = Form1.ActiveForm;
         wadd.Show();

    }

你只需要向后逻辑。如果没有打开表单,则创建它然后打开它。如果已有表格,则只需使用它。

答案 1 :(得分:0)

我想你想要这样的东西:

public Form FormOpened<T>()
    {
        foreach (Form form in Application.OpenForms)
            if (typeof(T) == form.GetType())
                return form;

        return null;
    }

然后您可以像这样使用它:

form f = (form)FormOpened<form>();
if (f2 == null)
{
    f2 = new form();
    f2.MdiParent = this;
    f2.Show();
}
else
{
    f2.Focus();
}

告诉我你是否需要任何帮助。