简体   繁体   English

在按钮上打开标签页单击Winform C#

[英]Open Tab Pages on Button Click in Winform C#

I have several tab pages collection. 我有几个标签页集合。 By default when user open the apps, the first tab page is the start tab page, then user will close the tab page. 默认情况下,当用户打开应用程序时,第一个标签页是开始标签页,然后用户将关闭该标签页。 Now I would like to create a situation where when the user go to the menu strip, click for example the "tab page 1 button", then the "tab page 1" will appear in the tab control. 现在我想创建一种情况,当用户转到菜单条时,单击“标签页1按钮”,然后“标签页1”将出现在选项卡控件中。 Any expertise can help me please... 任何专业知识都可以帮助我...

Use the SelectedTab() method. 使用SelectedTab()方法。 It has three overloads. 它有三个重载。


If you have a reference to the tab: 如果对选项卡有引用:

tabControl1.SelectTab(tabPage2);

If you only know the index: 如果只知道索引:

tabControl1.SelectTab(1);  // 0-based index, this shows the second tab

If you only know the name: 如果仅知道名称:

tabControl1.SelectTab("tabPage2");

You say your users can click an [x] that removes the tab. 您说用户可以单击删除选项卡的[x]

I'll assume it's removed by the easiest means, something like: 我假设它是通过最简单的方法删除的,例如:

tabControl1.TabPages.Remove(tabPage1);

You can't focus on a tab that's not part of the tab control, so you'll have to add it back first. 您不能专注于不属于选项卡控件的选项卡,因此必须先将其重新添加。

tabControl1.TabPages.Add(tabPage1);        // add tab as last tab in tabcontrol
tabControl1.TabPages.Insert(0, tabPage1);  // or insert it at a specific index

tabControl1.SelectTab(tabPage1);

To select the tab page of the TabPage control, not only could user click the title to switch pages, but set the selectedTabPageIndex property (or like this) to do it. 要选择TabPage控件的选项卡页面,用户不仅可以单击标题来切换页面,还可以设置selectedTabPageIndex属性(或者像这样)来执行此操作。

Just have a try. 试一试。

i am also facing this problem. 我也面临这个问题。 Finally i solve by following code. 最后我通过以下代码解决。 Scenario My tab Control have many tabs and i make a [x] sign for closing that tab. 方案我的选项卡控件有许多选项卡,并且我用[x]标记关闭了该选项卡。 on click [x] my tab is remove from Tab Control. 单击[x]时,我的标签页已从“标签页控件”中删除。 Now when i click on button, i open the tab (that was Removed) Code 现在,当我单击按钮时,我打开选项卡(已删除) 代码

private void openProductTab_Click(object sender, EventArgs e)
{
 if (tabControlMdi.TabPages.Contains(tabProduct))//tab already present
  {
    tabControlMdi.SelectTab(tabProduct);  // select by name
  }
 else
  {
    tabControlMdi.TabPages.Add(tabProduct); // add removed tab
    tabControlMdi.SelectTab(tabProduct);    // select by name
  }
}
 private void invoiceGenerationToolStripMenuItem_Click(object sender, EventArgs e)
    {
        foreach (Form form in Application.OpenForms)
        {
            if (form.GetType() == typeof(RETransactions.frmInvoicegeneration))
            {
                form.Activate();
                foreach (TabPage item in tabControl1.TabPages)
                {
                    if (item.Text == "Invoice Generation")
                    {
                        tabControl1.SelectTab(item);
                    }
                }
                return;
            }
        }
        RETransactions.frmInvoicegeneration rTenancy = new RETransactions.frmInvoicegeneration();
        rTenancy.Show();
        rTenancy.TopLevel = false;
        TabPage tabp = new TabPage("Invoice Generation");
        tabp.Controls.Add(rTenancy);
        tabControl1.TabPages.Add(tabp);
        tabControl1.SelectTab(tabp);
        tabp.BackColor = Color.Gainsboro;
    }

// i hope it will work ... thank you //我希望它能起作用...谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM