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
}