Hi smile,
Check this sample. now please take its reference.
Code
C#
Role.CS
static class Role
{
public static string UserRole;
}
Form1
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen();
}
private void btnLogin_Click(object sender, EventArgs e)
{
Role.UserRole = txtUser.Text.Trim();
MainForm mainForm = new MainForm();
mainForm.Show();
}
MainForm
private void MainForm_Load(object sender, EventArgs e)
{
this.CenterToScreen();
DataTable dt = new DataTable();
dt.Columns.Add("G_Name");
dt.Columns.Add("UserName");
dt.Columns.Add("FormCaption");
dt.Columns.Add("fCtrlName");
dt.Columns.Add("IsRights");
dt.Rows.Add("User", "User", "Student Inquiry", "View", 1);
dt.Rows.Add("User", "User", "Student Registeration", "Save", 1);
dt.Rows.Add("Admin", "Admin", "Student Attendance", "Update", 1);
dt.Rows.Add("Admin", "Admin", "Stdent List", "Delete", 1);
dt.Rows.Add("Admin", "Admin", "Student Promotion", "Export", 1);
DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
foreach (ToolStripDropDownItem item in toolStripMenuItem.DropDownItems)
{
item.Visible = false;
foreach (DataRow dr in rows)
{
if (dr["FormCaption"].ToString() == item.Text)
{
item.Visible = true;
break;
}
}
}
}
private void studentInquiryToolStripMenuItem_Click(object sender, EventArgs e)
{
frmStuRegister form = new frmStuRegister();
form.Show();
}
private void studentRegisterationToolStripMenuItem_Click(object sender, EventArgs e)
{
frmStudentEnquiry form = new frmStudentEnquiry();
form.Show();
}
private void studentAttendanceToolStripMenuItem_Click(object sender, EventArgs e)
{
frmStudentAttendence form = new frmStudentAttendence();
form.Show();
}
private void stdentListToolStripMenuItem_Click(object sender, EventArgs e)
{
frmStudentList form = new frmStudentList();
form.Show();
}
private void studentPromotionToolStripMenuItem_Click(object sender, EventArgs e)
{
frmStudentPromotion form = new frmStudentPromotion();
form.Show();
}
StudentRegisterForm
private void frmStuRegister_Load(object sender, EventArgs e)
{
this.CenterToScreen();
DataTable dt = new DataTable();
dt.Columns.Add("G_Name");
dt.Columns.Add("UserName");
dt.Columns.Add("FormCaption");
dt.Columns.Add("fCtrlName");
dt.Columns.Add("IsRights");
dt.Rows.Add("User", "User", "Student Inquiry", "View", 1);
dt.Rows.Add("User", "User", "Student Inquiry", "Save", 1);
DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
foreach (DataRow dr in rows)
{
string button = "btn" + dr["fCtrlName"].ToString();
Control btn = this.Controls.Find(button, false)[0];
btn.Visible = true;
}
StudentEnquiryForm
private void frmStudentEnquiry_Load(object sender, EventArgs e)
{
this.CenterToScreen();
DataTable dt = new DataTable();
dt.Columns.Add("G_Name");
dt.Columns.Add("UserName");
dt.Columns.Add("FormCaption");
dt.Columns.Add("fCtrlName");
dt.Columns.Add("IsRights");
dt.Rows.Add("User", "User", "Student Inquiry", "View", 1);
dt.Rows.Add("User", "User", "Student Inquiry", "Save", 1);
DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
foreach (DataRow dr in rows)
{
string button = "btn" + dr["fCtrlName"].ToString();
Control btn = this.Controls.Find(button, false)[0];
btn.Visible = true;
}
}
StudentAttendenceForm
private void frmStudentAttendence_Load(object sender, EventArgs e)
{
this.CenterToScreen();
DataTable dt = new DataTable();
dt.Columns.Add("G_Name");
dt.Columns.Add("UserName");
dt.Columns.Add("FormCaption");
dt.Columns.Add("fCtrlName");
dt.Columns.Add("IsRights");
dt.Rows.Add("Admin", "Admin", "Student Attendance", "View", 1);
dt.Rows.Add("Admin", "Admin", "Student Attendance", "Save", 1);
dt.Rows.Add("Admin", "Admin", "Student Attendance", "Update", 1);
dt.Rows.Add("Admin", "Admin", "Student Attendance", "Delete", 1);
dt.Rows.Add("Admin", "Admin", "Student Attendance", "Export", 1);
DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
foreach (DataRow dr in rows)
{
string button = "btn" + dr["fCtrlName"].ToString();
Control btn = this.Controls.Find(button, false)[0];
btn.Visible = true;
}
}
StudentListForm
private void frmStudentList_Load(object sender, EventArgs e)
{
this.CenterToScreen();
DataTable dt = new DataTable();
dt.Columns.Add("G_Name");
dt.Columns.Add("UserName");
dt.Columns.Add("FormCaption");
dt.Columns.Add("fCtrlName");
dt.Columns.Add("IsRights");
dt.Rows.Add("Admin", "Admin", "Student List", "View", 1);
dt.Rows.Add("Admin", "Admin", "Student List", "Save", 1);
dt.Rows.Add("Admin", "Admin", "Student List", "Update", 1);
dt.Rows.Add("Admin", "Admin", "Student List", "Delete", 1);
dt.Rows.Add("Admin", "Admin", "Student List", "Export", 1);
DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
foreach (DataRow dr in rows)
{
string button = "btn" + dr["fCtrlName"].ToString();
Control btn = this.Controls.Find(button, false)[0];
btn.Visible = true;
}
}
StudentPromotionForm
private void frmStudentPromotion_Load(object sender, EventArgs e)
{
this.CenterToScreen();
DataTable dt = new DataTable();
dt.Columns.Add("G_Name");
dt.Columns.Add("UserName");
dt.Columns.Add("FormCaption");
dt.Columns.Add("fCtrlName");
dt.Columns.Add("IsRights");
dt.Rows.Add("Admin", "Admin", "Student Promotion", "Export", 1);
DataRow[] rows = dt.Select("G_Name='" + Role.UserRole.ToLower() + "'");
foreach (DataRow dr in rows)
{
string button = "btn" + dr["fCtrlName"].ToString();
Control btn = this.Controls.Find(button, false)[0];
btn.Visible = true;
}
}
Screenshot