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?

nathanchere
  • 8,008
  • 15
  • 65
  • 86
  • Possible duplicate: http://stackoverflow.com/questions/552579/how-to-hide-tabpage-from-tabcontrol – prostynick Oct 22 '10 at 08:31
  • possible duplicate of [Hiding and Showing TabPages in tabControl](http://stackoverflow.com/questions/3365025/hiding-and-showing-tabpages-in-tabcontrol) – Hans Passant Oct 22 '10 at 08:41

3 Answers3

1
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
  • 2,662
  • 3
  • 24
  • 33
  • 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 Oct 22 '10 at 08:40
  • @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 Oct 22 '10 at 08:50
  • 1
    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 Oct 22 '10 at 08:53
  • What else has a reference to the tabPage2 when the TabControl and form are removed? – Paw Baltzersen Oct 22 '10 at 08:55
  • @Hans: You do realize C# uses garbage collection..? :-) – Jay Dec 03 '12 at 06:13
  • 1
    The hidden "parking window" has a reference to it. Causing the tab page and all the controls on it to leak forever. Garbage collection doesn't fix it. – Hans Passant Dec 03 '12 at 08:14
0

No its not possible to hide a tab in tabcontrol. If you are adding the tabs ar run time then dont add 7th tab if condition not satisfied.

If you done in design time then remove the tab if condtion failed.

yourTabControl.TabPages.Remove(tabPageName);
andy
  • 5,979
  • 2
  • 27
  • 49
0

you can implement a property

public bool TabVisible
{
    get 
    {
        return tabControl1.Contains(tabPage2);
    }
    set
    { 
        if(value == TabVisible) return;
        if(value)
            tabControl1.TabPages.Add(tabPage2);
        else
            tabControl1.TabPages.Remove(tabPage2);
    }
}

you should also overwrite your disposing function,

you can move out the Dispose function from the designer generated code to your own code, the designer notices that. you see that the components.Dispose(); function can not reach the tabPage any more for disposal, so you need to dispose it manually if it has not been disposed. otherways, especially if you are opening your window many times, you run out of window handles

user287107
  • 9,286
  • 1
  • 31
  • 47