Avatar of Eduardo Fuerte
Eduardo Fuerte
Flag for Brazil asked on

Could you point how to desabilitate a C# Windows Form tab page in a manner that it could't even be opened?

Hi Experts

Could you point how to desabilitate a C# Windows Form tab page in a manner that it could't even be opened?

By using this code
 // Desabilita as tab_pages
                (tbcPublicacoes.TabPages[1] as Control).Enabled = false;
                (tbcPublicacoes.TabPages[2] as Control).Enabled = false;
                (tbcPublicacoes.TabPages[3] as Control).Enabled = false;
                (tbcPublicacoes.TabPages[4] as Control).Enabled = false;

Open in new window

Even the control inside the tabpage is disabled, the pages could still been opened:
What I need (extracted from a VFP project)
Thanks in advance
C#FoxPro.NET Programming

Avatar of undefined
Last Comment
Eduardo Fuerte

8/22/2022 - Mon
Fernando Soto

Hi  Eduardo;

The TabPages of the TabControl can not be made unselectable only the controls on the specific page can all be made unselectable. Once a TabPage is part of the TabControl TabPage collection it will be displayed. What you can do is have a class level variable of type TabPage assign the page to that variable and remove it from the TabControl TabPages collection. This will cause the TabPage not to show at all. Not what you may want but only way I know to do what  I think you want.
Robert Schutt

There are some more options:

1) you can use owner drawn items to give the tabs a disabled look;
2) you can use the Selecting event to stop the user from selecting disabled tabs.

These can also have some negative side effects:
1) the default windows background is not drawn anymore, I have tried to cater for that partially by using a gradient (can be removed easily if you don't want that);
2) if there is an enabled tab between 2 disabled tabs then you can't reach it with keys, only mouse (maybe not relevant for you).

In the code below I have built on the fact that you already have the code to set tabs disabled (and I assume you set enabled to true when you want them to become available again).
        private void Form1_Load(object sender, EventArgs e) {

            // set tabpages you want as disabled
            (tabControl1.TabPages[1] as Control).Enabled = false;

            // owner drawn items to give the impression of really being disabled
            tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);

            // do not allow the disabled tabs to be selected
            tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);

        void tabControl1_DrawItem(object sender, DrawItemEventArgs e) {
            TabControl tc = (TabControl)sender;
            TabPage tp = tc.TabPages[e.Index];
            Graphics gr = e.Graphics;
            Rectangle rect = e.Bounds;
            // draw background, color depends on selected item or not
            Brush bg = (e.State == DrawItemState.Selected) ? Brushes.White : new System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.FromArgb(255, 241, 241, 241), Color.FromArgb(255, 206, 206, 206), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
            gr.FillRectangle(bg, rect);
            // draw tab name, color depends on disabled or not
            string txt = tp.Text;
            Color col = tp.Enabled ? tp.ForeColor : Color.Gray; // normal / 'disabled'
            rect.Offset(0, 3);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            gr.DrawString(txt, tp.Font, new SolidBrush(col), rect, sf);

        private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e) {
            // if tab page is disabled, then cancel the selection event
            if (e.Action == TabControlAction.Selecting && !e.TabPage.Enabled) {
                e.Cancel = true;

Open in new window

Here is a little preview:
Éric Moreau

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
Eduardo Fuerte

Hi Robert

The form really starts the way I need using your code

But after saving the first tab's data the 2nd tab must be enabled. I coded this:

           //Include data
// Inform success
                    MessageBox.Show("Book Included.", "Incluir", MessageBoxButtons.OK, MessageBoxIcon.Information);

// So, the 2nd tab page must be enabled
                    (tbcPublicacoes.TabPages[1] as Control).Enabled = true;

Open in new window

But the 2nd tab isn't enabled. What else must be done?
Robert Schutt

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Eduardo Fuerte

This solution attends what I need.
Thank you for the assistance!