Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: Intermediate
Jun 8, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Hiding pages of a TabControl

Sometimes you may need to hide or show some pages of a TabControl according to the information you want to show, basing on the currently logged-in user for example (users with low privileges won't be able to get all the information or to change records). To do this, you may attempt to add or remove the pages to and from the TabControl's TabPages collection. This approach seems to work fine at first, until unfortunately you realize that after you've removed a page from the collection, you can no longer close the form. It doesn't matter what you do - press CTRL+F4, click the Close item of the form's system menu or the X icon on the title bar - the form won't close.

Apparently there is a bug that prevents to close the form if any of the child TabPage instances doesn't have a parent TabControl. However, what's interesting is that if you remove a page from a TabControl and add it to another TabControl, then the form closes correctly.

So, when you need to hide a page from your TabControl, just add it to another invisible TabControl (set its Visible property to False). You don't even need to explicitly remove the page from the first control, this will be automatically done when the page is added to the second control's TabPages collection. The following line shows how to do this:

Next, when you want to re-show the page, you have to re-add it to the original control. Be careful doing this though, because if you just add the page as we did in the previous line of code the page will be added after the existent pages, not in its original position. The TabPages collection doesn't expose an Insert method that allows you to insert the page in the desired position, as other collections do. A simple work around to this last issue is to remove all the pages from the first TabControl, and then add them all back in the right order. Here it is an example:

' remove all the pages
' add all the pages in the right order
TabControl1.TabPages.AddRange(New TabPage() {pageToHide, page2, page3})
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date