I don't really need to disable them because I either disable the TabControl
or enable it. But when the TabControl
is disabled, I want the tab pages look disabled (greyed out).
Asked
Active
Viewed 1.0k times
3

Joan Venge
- 315,713
- 212
- 479
- 689
-
possible duplicate: http://stackoverflow.com/questions/418006/how-can-i-disable-a-tab-inside-a-tabcontrol. The best answer to that question is http://stackoverflow.com/a/418033/635634 – M.Babcock Jan 31 '12 at 22:16
-
Yes sorry forgot to mention that. – Joan Venge Jan 31 '12 at 22:18
-
But it says the header will still be enabled. I assumed it will look enabled still which is what I am trying to solve. – Joan Venge Jan 31 '12 at 22:19
-
@JoanVenge - The standard winforms tab control doesn't provide that much flexibility. – M.Babcock Jan 31 '12 at 22:21
-
If you do it from the Controls level yes use a something like this foreach (Control control in ctls) { control.Enabled = enable; then you may need to call the method recursively if there are multiple controls – MethodMan Jan 31 '12 at 22:23
-
Check my example. It combines all 3 methods mentioned in http://stackoverflow.com/questions/418006/how-can-i-disable-a-tab-inside-a-tabcontrol/418033 to do what you need. – corylulu Jan 31 '12 at 22:25
3 Answers
6
What people have mentioned below won't do the trick individually, but combined they will. Try this out:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Disable tabPage2
this.tabPage2.Enabled = false; // no casting required.
this.tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new DrawItemEventHandler(DisableTab_DrawItem);
}
private void DisableTab_DrawItem(object sender, DrawItemEventArgs e)
{
TabControl tabControl = sender as TabControl;
TabPage page = tabControl.TabPages[e.Index];
if (!page.Enabled)
{
//Draws disabled tab
using (SolidBrush brush = new SolidBrush(SystemColors.GrayText))
{
e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
}
}
else
{
// Draws normal tab
using (SolidBrush brush = new SolidBrush(page.ForeColor))
{
e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
}
}
}
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
//Cancels click on disabled tab.
if (!e.TabPage.Enabled)
e.Cancel = true;
}
}

corylulu
- 3,449
- 1
- 19
- 35
-
Thanks, I just tried but I don't see the Enabled property in tabpage controls. How did you get yours? – Joan Venge Jan 31 '12 at 22:35
-
You can cast it as well. ((Control)tabPage2).Enabled = false; I've tested both ways and they work. – corylulu Jan 31 '12 at 22:43
-
http://dl.dropbox.com/u/18919663/vs%20samples/disable%20tab%20test.zip Here is my project I tested this in. – corylulu Jan 31 '12 at 22:45
-
Ok I just tried it but it doesn't disable the tab header itself. The controls inside the tab are disabled but the tab pages look as if they are enabled. – Joan Venge Jan 31 '12 at 22:48
-
Did you try my sample? Mine disables the header tab as well. That's what does: this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed; this.tabControl1.DrawItem += new DrawItemEventHandler(DisableTab_DrawItem); – corylulu Jan 31 '12 at 22:50
-
Thanks for the sample file. Your tabs do look disabled, in the app (Photoshop) I am launcging this app, the tabs look old-win32 style, i.e. solid gray color not the win7 style. So I guess this is a different issue. – Joan Venge Jan 31 '12 at 22:51
-
Try adjusting the: new SolidBrush(SystemColors.GrayText) and try different colors – corylulu Jan 31 '12 at 22:53
-
Thanks, same result, it didn't make a different when I set it to red. – Joan Venge Jan 31 '12 at 22:58
-
I noticed your tab pages use UseVisualBackStyle to True, will try that. – Joan Venge Jan 31 '12 at 23:00
-
-
You also need to make sure you add: Application.EnableVisualStyles(); to your Program.cs page. – corylulu Jan 31 '12 at 23:06
-
-
Thanks, I would but I think photoshop is doing weird stuff to apps that are children of it, so it wouldn't make a difference I think. I have EnableVisualStyles in Program.cs though. – Joan Venge Jan 31 '12 at 23:20
3
Try using This code it works:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage == tabControl1.TabPages[1])
{
e.Cancel = true;
}
}
Keep the index or name of the tabpage you want to disable in the if condition here i have kept index as 1. :)

lokendra jayaswal
- 298
- 1
- 6
- 19
2
Rewrote solution provided by @Corylulu to incapsulate everything in a control itself.
public class DimmableTabControl : TabControl
{
public DimmableTabControl()
{
DrawMode = TabDrawMode.OwnerDrawFixed;
DrawItem += DimmableTabControl_DrawItem;
Selecting += DimmableTabControl_Selecting;
}
private void DimmableTabControl_DrawItem(object sender, DrawItemEventArgs e)
{
TabPage page = TabPages[e.Index];
using(SolidBrush brush = new SolidBrush(page.Enabled ? page.ForeColor : SystemColors.GrayText))
{
e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
}
}
private void DimmableTabControl_Selecting(object sender, TabControlCancelEventArgs e)
{
if(!e.TabPage.Enabled)
{
e.Cancel = true;
}
}
}

antgraf
- 155
- 2
- 7