2

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...

Ren
  • 765
  • 4
  • 15
  • 42
  • I have created the tab pages collection. for instance tabpage1,tabpage2,tab page 3. Also have button page1,page2,page3. Now i want to create something like if user click button page1,the tabpage1 will appear on the tab control. – Ren Feb 11 '14 at 02:17

4 Answers4

9

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);
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • It's working if the user did not close the tab, but the case when user already close the tab, and want to open it again by clicking the button. This error appear: Additional information: Value cannot be null. – Ren Feb 11 '14 at 02:42
  • I create [x] at the tab header that will allow the user to close tab. Now I want the user to open it again by clicking the button. – Ren Feb 11 '14 at 02:52
  • yup, I render it. you can refer to this: http://stackoverflow.com/questions/3183352/close-button-in-tabcontrol – Ren Feb 11 '14 at 03:06
  • Yes, if you got the solution please let me know. Look for this solution for many days. – Ren Feb 11 '14 at 03:15
0

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.

Itachi
  • 5,777
  • 2
  • 37
  • 69
0

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
  }
}
0
 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

  • 1
    Thank you for contributing to Stack Overflow. It would help readers if you added some text explaining your code and why your solution is a good one. Code only answers are discouraged as they do not help people learn and improve.You can use the [edit] button to improve this answer to get more votes and reputation! – Brian Tompsett - 汤莱恩 Jun 03 '18 at 12:09