views:

797

answers:

3

I have a TabControl with 5 tabs, and the contents of one of the tabs depends on some conditions or modes - Sometimes it needs to show one set of controls, on other times it should show an entirely different set of controls.

What is the easiest way to achieve this? I tried setting up two different tabs, and using something like tab.Enabled / Visible - but couldn't find such attributes on the tabs.

I would like the ability the switch modes of operation - go from displaying one tab, to displaying the other tab, and back again. Mind you, I don't want to change which tab is active, I want to completely hide one tab, then show it and hide another tab.

+1  A: 

Here is a simple fix (hack)

http://www.dotnetspider.com/resources/18344-Hiding-Showing-Tabpages-Tabcontrol.aspx

A: 

I don't know of an elegant solution. The easiest for a small application seems to be to TabPages.Add/Insert/Remove as suggested above.

In our large application, we override the TabControl and the TabPage class-- call them MyTabControl and MyTabPage-- and added our own Visible property. Setting tabPage.Visible = false causes the tab page to remove itself from the tabControl.TabPages collection. Making the page visible again causes it to insert itself to the tab control's collection at the original index. The Visible property made the rest of our code a little easier to manage.

However, the subclasses made our MyTabControl subclass slightly harder to design. TabControl.TabPages is a TabPageCollection. The Visual Studio designer wants to create and add TabPages to it. We needed it to hold our MyTabPage subclasses, so we also created a MyTabPageCollection.

We also have many places where different groups of controls on a tab are visible depending on user choices. We group controls on different panels and show the panel that corresponds to the appropriate settings settings.

Paul Williams
+1  A: 

I can't believe I haven't seen a proper solution for something so easy. I know in the past I've shown and hidden the tabs.

tabControl.SelectTab(index);

Bingo! No remove, no insert and no shuffling.

-Holt

Holt