Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag 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:
User generated image
What I need (extracted from a VFP project)
User generated image
Thanks in advance
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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.
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:
User generated image
Avatar of Eduardo Fuerte

ASKER

Hi Robert

The form really starts the way I need using your code

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

           //Include data
                     publicacaoBLL.Incluir(_publicacao);
// 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?
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This solution attends what I need.
Thank you for the assistance!