Craig Johnston
Craig Johnston

Reputation: 21

how to set a TabControl tab to be invisible

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?

Upvotes: 2

Views: 6884

Answers (3)

user287107
user287107

Reputation: 9417

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

Upvotes: 0

andy
andy

Reputation: 6079

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

Upvotes: 0

Paw Baltzersen
Paw Baltzersen

Reputation: 2752

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.

Upvotes: 1

Related Questions