Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding and Showing TabPages in tabControl

I am trying to show or hide tabpages as per user choice. If user selects gender male then form for male in a tabpage "male" should be displayed and if user selects female then similar next form should be displayed in next tab "female"

I tried using

tabControl1.TabPages.Remove(...) 

and

tabControl1.TabPages.Add(...) 

It adds and removes the tabpages but doing so will loose my controls on tabpages too... i can't see them back. what's the problem here?

like image 416
KoolKabin Avatar asked Jul 29 '10 16:07

KoolKabin


People also ask

How to hide TabPage in TabControl?

To hide a tab: tabControl. TabPages. Remove(tabPage);

How do you hide and show a TabPage in a TabControl in VB net?

To hide tabs in IntegralUI TabControl is simple, just set the Visible property value to false, and the designated tab will become hidden. The tab still exists in Pages collecton of TabControl, only it is not shown in tab strip.

How do I hide my TabPage?

The Hide method of the TabPage will not hide the tab. To hide the tab, you must remove the TabPage control from the TabControl..::. TabPages collection.


2 Answers

I think the answer is much easier.

To hide the tab you just can use the way you already tried or adressing the TabPage itself.

TabControl1.TabPages.Remove(TabPage1) 'Could be male TabControl1.TabPages.Remove(TabPage2) 'Could be female 

a.s.o.

Removing the TabPage does not destroy it and the controls on it. To show the corresponding tab again just use the following code

TabControl1.TabPages.Insert(0, TabPage1) 'Show male TabControl1.TabPages.Insert(1, TabPage2) 'Show female 
like image 158
Mike Avatar answered Sep 28 '22 03:09

Mike


You could remove the tab page from the TabControl.TabPages collection and store it in a list. For example:

    private List<TabPage> hiddenPages = new List<TabPage>();      private void EnablePage(TabPage page, bool enable) {         if (enable) {             tabControl1.TabPages.Add(page);             hiddenPages.Remove(page);         }         else {             tabControl1.TabPages.Remove(page);             hiddenPages.Add(page);         }     }      protected override void OnFormClosed(FormClosedEventArgs e) {         foreach (var page in hiddenPages) page.Dispose();         base.OnFormClosed(e);     } 
like image 29
Hans Passant Avatar answered Sep 28 '22 03:09

Hans Passant