Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to disable a TabPage inside a TabControl?

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).

like image 971
Joan Venge Avatar asked Nov 21 '25 10:11

Joan Venge


2 Answers

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;
    }
}
like image 65
corylulu Avatar answered Nov 23 '25 23:11

corylulu


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. :)

like image 39
lokendra jayaswal Avatar answered Nov 23 '25 23:11

lokendra jayaswal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!