tags:

views:

3549

answers:

8

Is there any way to disable a tab in a tabcontrol?

I am using C#.

+7  A: 

Cast your TabPage to a Control, then set the Enabled property to false.

(this.tabPage as Control).Enabled = false;

Therefore, the tabpage's header will still be enabled but its contents will be disabled.

Cedrik
+3  A: 

The TabPage class doesn't have an Enable property. You can get a similar effect simply by setting the Enable property of the controls on that page. That also avoids the problem of dealing with a TabControl that has only one page. For example:

public static void EnableTab(TabPage page, bool enable) {
  EnableControls(page.Controls, enable);
}
private static void EnableControls(Control.ControlCollection ctls, bool enable) {
  foreach (Control ctl in ctls) {
    ctl.Enabled = enable;
    EnableControls(ctl.Controls, enable);
  }
}
Hans Passant
+1  A: 

The only way is to catch the Selecting event and prevent a tab from being activated.

Martijn Laarman
A: 

I've removed tab pages in the past to prevent the user from clicking them. This probably isn't the best solution though because they may need to see that the tab page exists.

Aaron Smith
+4  A: 

You could register the "Selecting" event and cancel the navigation to the tab page:

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
    if (e.TabPage == tabPage2)
        e.Cancel = true;
}

Another idea is to put all the controls on the tabpage in a Panel control and disable the panel! Smiley

You could also remove the tabpage from the tabCopntrol1.TabPages collection. That would hide the tabpage.

credits go to littleguru @ channel 9

Stormenet
More complete and the post i should have posted :)
Martijn Laarman
+1  A: 

I had to handle this a while back. I removed the Tab from the TabPages collection (I think that's it) and added it back in when the conditions changed. But that was only in Winforms where I could keep the tab around until I needed it again.

jcollum
+2  A: 

Presumably, you want to see the tab in the tab control, but you want it to be "disabled" (i.e., greyed, and unselectable). There is no built-in support for this, but you can override the drawing mechanism to give the desired effect.

An example of how to do this is provided here.

The magic is in this snippet from the presented source, and in the DisableTab_DrawItem method:

this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new DrawItemEventHandler( DisableTab_DrawItem );
Stewart
A: 

MyTabControl.SelectedTab.Enabled = false;

baeltazor