LIDOR SYSTEMS

Advanced User Interface Controls and Components

Hide or Disable Tab Pages in TabControl .NET

Created: 05 November 2013

In general all tabs in TabControl are visible and enabled. However, in some cases you may need to hide some tabs or disable them. This is useful to create an UI where tab visibility or function is determined by user preferences.

Hide or Disable Tabs in TabCOntrol .NET
Download Sample - Hide or Disable Tabs in TabControl .NET

How to Hide Tabs

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. Here is an example where we are making some tab invisible:

// Hide a specific tab header

page.Visible = false;

' Hide a specific tab header

page.Visible = False

All tabs can become invisible, the only exception is the currently selected tab which will remain visible, that is until the selection is changed to some other tab. In order to handle this situation, we will first change the current selection, and then hide the specified tab. For this purpose we have create a method which will return another visible and enabled tab page, which will become selected.

private LidorSystems.IntegralUI.Containers.TabPage GetPrevSelectedPage(LidorSystems.IntegralUI.Containers.TabPage page)

{

// Get the page index in Pages collection

int pageIndex = page.Index;

 

// At first search among all previous tabs to find some visible and enabled tab

for (int i = pageIndex - 1; i >= 0; i--)

{

if (this.tabControl1.Pages[i].Visible && this.tabControl1.Pages[i].Enabled)

return this.tabControl1.Pages[i];

}

 

// If a tab is not found, look among all other tabs and find some visible and enabled tab

for (int i = pageIndex + 1; i < this.tabControl1.Pages.Count; i++)

{

if (this.tabControl1.Pages[i].Visible && this.tabControl1.Pages[i].Enabled)

return this.tabControl1.Pages[i];

}

 

// Return null only if there is no visible and enabled tab

return null;

}

Private Function GetPrevSelectedPage(page As LidorSystems.IntegralUI.Containers.TabPage) As LidorSystems.IntegralUI.Containers.TabPage

' Get the page index in Pages collection

Dim pageIndex As Integer = page.Index

 

' At first search among all previous tabs to find some visible and enabled tab

For i As Integer = pageIndex - 1 To 0 Step -1

If Me.tabControl1.Pages(i).Visible AndAlso Me.tabControl1.Pages(i).Enabled Then

Return Me.tabControl1.Pages(i)

End If

Next

 

' If a tab is not found, look among all other tabs and find some visible and enabled tab

For i As Integer = pageIndex + 1 To Me.tabControl1.Pages.Count - 1

If Me.tabControl1.Pages(i).Visible AndAlso Me.tabControl1.Pages(i).Enabled Then

Return Me.tabControl1.Pages(i)

End If

Next

 

' Return null only if there is no visible and enabled tab

Return Nothing

End Function

You can change this method and use any other way to select other tabs. Now, when we have found some tab that could become selected, we can change the selection and make the original tab hidden.

// If original tab page is equal to currently selected tab page, find a replacement

if (page == this.tabControl1.SelectedPage && (!cbVisible.Checked || !cbEnabled.Checked))

this.tabControl1.SelectedPage = GetPrevSelectedPage(page);

 

// Then change the tab visibility

page.Visible = cbVisible.Checked;

' If original tab page is equal to currently selected tab page, find a replacement

If page Is Me.tabControl1.SelectedPage AndAlso (Not cbVisible.Checked OrElse Not cbEnabled.Checked) Then

Me.tabControl1.SelectedPage = GetPrevSelectedPage(page)

End If

 

' Then change the tab visibility

page.Visible = cbVisible.Checked

How to Disable Tabs

In similar way to hide tabs, we can also make them disabled. To do that, we are using the Enabled property. Here is an example on how to disable tab:

// Disable a specific tab header

page.Enabled = false;

' Disable a specific tab header

page.Enabled = False

Like in previous case, all tabs can become disabled, however if we are making currently selected tab disabled, the content of that tab cannot be shown. So we need to make sure to change the selection to some other visible and enabled tab, prior disabling the original tab. In this case we can also use the GetPrevSelectedPage method to find some tab. After that we can disable the original tab:

// If original tab page is equal to currently selected tab page, find a replacement

if (page == this.tabControl1.SelectedPage && (!cbVisible.Checked || !cbEnabled.Checked))

this.tabControl1.SelectedPage = GetPrevSelectedPage(page);

 

// Then disable the tab

page.Enabled = cbEnabled.Checked;

' If original tab page is equal to currently selected tab page, find a replacement

If page Is Me.tabControl1.SelectedPage AndAlso (Not cbVisible.Checked OrElse Not cbEnabled.Checked) Then

Me.tabControl1.SelectedPage = GetPrevSelectedPage(page)

End If

 

' Then disable the tab

page.Enabled = cbEnabled.Checked

When a tab become hidden or disabled, it cannot have an input focus and cannot be clicked. The content of that tab page will remain hidden, until it becomes visible and enabled again.

Here is a sample project in C# and VB available for download from here: Hide or Disable Tabs in TabControl .NET

Did you Like this Article?


Enter your e-mail address below and you will receive latest articles as well as news on upcoming events and special offers.