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