Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

How To Hide Or Show .NET Tabs Programmatically

Rate me:
Please Sign up or sign in to vote.
2.80/5 (5 votes)
13 Nov 2011CPOL3 min read 53.6K   7   10
This article describes how to hide or show TabControl tabs at runtime


Introduction


Anyone who has used the .NET Winforms TabControl may have come across what seems to be a potentially serious shortcoming of that control - you cannot programmatically hide or show tabs! This was always very easy to do in the legacy VB6 days because there was an appropriate property to hide/show each tab, but with .NET it became much more difficult.

It would appear to be the case that the only way to hide a tab in .NET is to programmatically delete it from the TabControl tab collection, which is all very well - but if you have deleted a tab which you then wanted to show, you have to create it from scratch - and given that a single tab could potentially contain dozens of controls, that could be an expensive undertaking.


This article demonstrates how to easily hide/show a tab without having to recreate it from scratch. So you can design the tab in design mode, and then flip it between hidden and visible status in just one line of code. This keeps things simple.


Background


The .NET TabControl does not provide properties for hiding/showing tabs - you have to delete and recreate the tab, which can be programmatically expensive.

Using the Code


If you want to programmatically hide/show tabs on the .NET TabControl, all you need to do is create a second TabControl on your form, and set the Visible property of that second TabControl to false - meaning it won't be displayed to the user. In the following, my visible TabControl is called TabCtlVisible, and (surprise, surprise) the hidden TabControl is called TabCtlHidden.

Then for the tab(s) which you wish to programmatically hide/show, set the Parent property of the tab to either the visible TabControl, or the non-visible TabControl.


C#
bool TabIsVisible = true;

MyTab.Parent = (TabIsVisible) ? TabCtlVisible : TabCtlHidden;

If the tab has its Parent property set to the visible TabControl, it will be shown to the user. However if the Parent property is set to the non-visible TabControl then the tab will exist, but the user won't be able to see or interact with it.

Tabs can have their parent redefined at runtime, no problem. You could probably copy the tab you want to hide to an in-memory object instead of a hidden TabControl so that it can be recreated on the visible TabControl at will, but why make life difficult?


This approach could be really handy during debugging where you have an extra tab which you use for debugging your application (say for defining some test values), but you hide that tab when you compile the application in release mode (or alternatively use other program logic to make the tab hidden/visible).


Points of Interest


Note that the visible order of the tabs depend on the order in which they are hidden or shown. If a tab is hidden and then made visible, it will be returned to the visible TabControl to the right of other tabs already on display on the visible TabControl. That might not be what you intended.

If the order of the tabs is important to your application, you will need to implement some additional logic in your program to arrange the sort order of the tabs on the visible TabControl (you don't need to bother with the order of the hidden TabControl). This means changing the order of the contents of the tab collection for the visible TabControl.


History


First implementation

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I worked in the front line of the software development industry for over 20 years, previously I was a hardware engineer followed by applications consultant. For the last 10 years of my watch on software development I worked with the Microsoft .NET development framework, both in Winforms and with ASP.NET.

However as I move into the evening of my life I have been forced to take early retirement. Ii has recently been confirmed that I am afflicted with Parkinson's. This has been developing for some time, during which my hand tremors gradually deteriorated to the point where using a computer mouse became impossible. Trying to point/click on a tiny icon or menu option when your hands figure they can do a merry dance ultimately becomes intensely frustrating, and earning a living as a software developer reaches the stage of impossibility.

Comments and Discussions

 
GeneralThnx.... Pin
kpmagheswari20-May-13 8:27
kpmagheswari20-May-13 8:27 
GeneralRe: Thnx.... Pin
KazMaxLtd20-May-13 15:15
KazMaxLtd20-May-13 15:15 
GeneralThanks! Pin
Marcos Cardoso26-Mar-13 10:34
Marcos Cardoso26-Mar-13 10:34 
GeneralRe: Thanks! Pin
KazMaxLtd26-Mar-13 13:52
KazMaxLtd26-Mar-13 13:52 
QuestionThanks! Pin
KazMaxLtd28-May-12 23:03
KazMaxLtd28-May-12 23:03 
AnswerRe: Thanks! Indeed Thanks saves me from running into the ground on the missing hide tab. Pin
The Answer is 426-Nov-12 10:47
The Answer is 426-Nov-12 10:47 
GeneralRe: Thanks! Indeed Thanks saves me from running into the ground on the missing hide tab. Pin
KazMaxLtd7-Nov-12 1:10
KazMaxLtd7-Nov-12 1:10 
GeneralMy vote of 1 Pin
Generic Host28-May-12 19:18
Generic Host28-May-12 19:18 
GeneralReason for my vote of 2 Very inefficient. As the alternate s... Pin
fjdiewornncalwe21-Nov-11 6:51
professionalfjdiewornncalwe21-Nov-11 6:51 
GeneralReason for my vote of 1 sorry but this is nonsense, you are ... Pin
johannesnestler15-Nov-11 11:07
johannesnestler15-Nov-11 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.