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:

TabControl2.TabPages.Add(pageToHide)

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 pagesTabControl1.TabPages.Clear()' add all the pages in the right orderTabControl1.TabPages.AddRange(New TabPage() {pageToHide, page2, page3})

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.