Disable tab pages

  • Thread starter Thread starter emferrari
  • Start date Start date
E

emferrari

Hi

I have a program with a tabControl which contains 6 tabs. During the
execution of the program I want to Enable/Disable some of the tabs but
I did not find any way to do that. I've tried to use the Hide/Show
methods but without success.

Does anyone have any suggestion on this?

Thanks!

Eduardo
 
Eduardo,

Did you check the Enabled property on the TabPage class?
 
From
<http://msdn2.microsoft.com/en-us/library/system.windows.forms.tabpage.enabled.aspx>:

".NET Framework Class Library
TabPage.Enabled Property
This property supports the .NET Framework infrastructure and is not
intended to be used directly from your code.
This member is not meaningful for this control."

So, the tabcontrol doesn't really support this. There's lots of code on
the web to show you how to override the drawing of your tabcontrol and
disable tabs. In a pinch, it may be enough to handle the "Selecting"
event of the tabcontrol like this:

private void tabControl1_Selecting(object sender,
TabControlCancelEventArgs e) {
try {
if ( ! tabShouldBeEnabled(e.TabPageIndex) )
e.Cancel = true;
} catch (Exception ex) {
_bugSentry.Report(ex);
}
}
 
I had to do this and myapproach was to handle
TabControl.SelectedIndexChanged event
in this event I check if user can go on new index tab and if not I
switch them back to last tab and show appropriate message


I hope this helps
Galin Iliev[MCSD.NET]
www.galcho.com
 
I simply remove the relevant tab pages from the tab control when
they're disabled, and add them back when they're enabled.

The only hassle is that you can only add to the end, so it has to be
the last tab page(s) that is/are done like this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top