Question

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. Any expertise can help me please...

Was it helpful?

Solution

Use the SelectedTab() method. 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.

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

OTHER TIPS

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.

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. on click [x] my tab is remove from Tab Control. 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top