views:

56

answers:

2

In C# using VS2005 I have a Winforms TabControl with 7 tabs, but I want the last tab to be only visible if a certain configuration option is set.

How to make the TabControl only show the first six tabs? In other words, how do I make the seventh tab not visible?

A: 
private void HideTab(object sender, EventArgs e)
{
    this.tabControl1.TabPages.Remove(this.tabPage2);
}
private void ShowTab(object sender, EventArgs e)
{
    this.tabControl1.TabPages.Add(this.tabPage2);
}

this.tabPage2 is your 7th tabpage, whatever name you give it.

Paw Baltzersen
This isn't good enough, the removed page and its controls will leak permanently. Keeping track of removed pages and calling Dispose() when the form is closed is required.
Hans Passant
@Hans. Eh, no it wont if the form is closed. Unless you give the reference to some other objects. this.tabPage2 is referenced by the form containing it, when that form is closed the garbage collector will do the work for you.
Paw Baltzersen
No, TabControl.Dispose() will automatically dispose the pages. Which is called by Form.Dispose() iterating the Controls collection. But it can't when the page was removed. The tabPage2 reference isn't good enough, it isn't included in the Controls collection.
Hans Passant
What else has a reference to the tabPage2 when the TabControl and form are removed?
Paw Baltzersen
A: 
  private void removetab(string name)
    {
        for (int i = 0; i < tabControl1.TabPages.Count; i++)
        {
            if (tabControl1.TabPages[i].Name.Equals(name))
            {

                tabControl1.TabPages.RemoveAt(i);
            }

        }

    }
David