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).
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;
}
}
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. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With