0

I have found some implementation of the Enabled property for tabPages, since the implementation for it in the UI is hidden, but I do not know how to use it. It might strike as a stupid question, but I really just started with c# and the .net platform. I want to take a variable from the first form, which is a login form that sends a true value if one checkbox is checked or false if it's not, and if it is true, login as an admin that has the privillege to access all the tabs, but if it is false, a user will be restricted acces from some of the tabs. I do not know how to restrict access to these tabs, or allow it. The code is as follows:

Code for the constructor of the form:

public Form1()
{
InitializeComponent();

this.tabControl1.DrawMode = 
TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += 
new DrawItemEventHandler(DisableTab_DrawItem);
this.tabControl1.Selecting += 
new TabControlCancelEventHandler(DisableTab_Selecting);
}

The function which draws the tab pages:

/// <summary>
/// Draw a tab page based on whether it is disabled or enabled.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PageTab_DrawItem(object sender, DrawItemEventArgs e)
{
TabControl tabControl = sender as TabControl;
TabPage tabPage = tabControl.TabPages[e.Index];

if (tabPage.Enabled == false)
{
using (SolidBrush brush = 
new SolidBrush(SystemColors.GrayText))
{
e.Graphics.DrawString(tabPage.Text, tabPage.Font, brush, 
e.Bounds.X + 3, e.Bounds.Y + 3);
}
}
else
{
using (SolidBrush brush = new SolidBrush(tabPage.ForeColor))
{
e.Graphics.DrawString(tabPage.Text, tabPage.Font, brush, 
e.Bounds.X + 3, e.Bounds.Y + 3);
}
}
}

The event handler that prevents the tab from being selected if it is disabled:

/// <summary>
/// Cancel the selecting event if the TabPage is disabled.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PageTab_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage.Enabled == false)
{
e.Cancel = true;
}
}

The said variable and the decisional statement:

 int cont = Login.accountType; //the variable from the first tab, that decides whether the account is admin type or client type
        //the variable is public static int in the first form

        if (cont == 1)
        {
            //display all the tabs, because we logged in as admin
        }
        else if(cont == 0)
        {
            //disable the tabs that we do not want the client to access
        }
Cristian Baciu
  • 133
  • 1
  • 2
  • 15
  • The WinForms TabControl doesn't know what a disabled Tab is. Just remove the tabs that the users aren't supposed to see. – LarsTech May 10 '16 at 21:08
  • Yes but that would mean adding them back every time I load the TabControl and the admin has all the privilleges. – Cristian Baciu May 10 '16 at 21:11
  • So what's the problem with the code you posted? Where's the variable you are getting from the other form? – LarsTech May 10 '16 at 21:56

1 Answers1

0

I have learned how to use it, it does not show up in intellisense, that's what confused me at first, but it does work.

    int cont = Login.accountType; //the variable from the first tab, that decides whether the account is admin type or client type
    //the variable is public static int in the first form

    if (cont == 1)
    {
        tabControl1.TabPages[0].Enabled = true;
        tabControl1.TabPages[1].Enabled = true;
        tabControl1.TabPages[2].Enabled = true;
        tabControl1.TabPages[3].Enabled = true;
    }
    else if(cont == 0)
    {
        tabControl1.TabPages[1].Enabled = false;
    }

This will enable all the tabs for the admin, and it will disable for the client the one tab which contains the textboxes for the admin to insert new accounts into an ms-access database.

Cristian Baciu
  • 133
  • 1
  • 2
  • 15