Hi smile,
Check this example. Now please take its reference and correct your code.
Form Design
For retaining the role in every form i have added a class file.
C#
static class Role
{
public static string UserRole;
}
VB.Net
Module Role
Public UserRole As String
End Module
Code
C#
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
// Validate User and Assign role.
Role.UserRole = this.txtRole.Text.Trim();
MainForm mainForm = new MainForm();
mainForm.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
this.CenterToScreen();
}
}
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.CenterToScreen();
foreach (ToolStripDropDownItem item in toolStripMenuItem.DropDownItems)
{
item.Enabled = false;
switch (Role.UserRole.ToLower())
{
case "admin":
if (item.Text == "Student Register")
{
item.Enabled = true;
}
else if (item.Text == "Employee Register")
{
item.Enabled = true;
}
break;
case "abc":
if (item.Text == "Student Register")
{
item.Enabled = true;
}
else if (item.Text == "Employee Register")
{
item.Enabled = false;
}
break;
case "sky":
if (item.Text == "Student Register")
{
item.Enabled = false;
}
else if (item.Text == "Employee Register")
{
item.Enabled = true;
}
break;
}
}
}
private void studentRegisterToolStripMenuItem_Click(object sender, EventArgs e)
{
frmStuRegister formStudent = new frmStuRegister();
formStudent.Show();
}
private void employeeRegisterToolStripMenuItem_Click(object sender, EventArgs e)
{
frmEmpRegister formEmployee = new frmEmpRegister();
formEmployee.Show();
}
}
frmStuRegister.cs
public partial class frmStuRegister : Form
{
public frmStuRegister()
{
InitializeComponent();
}
private void frmStuRegister_Load(object sender, EventArgs e)
{
this.CenterToScreen();
switch (Role.UserRole.ToLower())
{
case "admin":
btnInsert.Enabled = true;
btnUpdate.Enabled = true;
btnDelete.Enabled = true;
btnView.Enabled = true;
btnExportExcel.Enabled = true;
btnPDF.Enabled = true;
break;
case "abc":
btnInsert.Enabled = true;
btnUpdate.Enabled = false;
btnDelete.Enabled = false;
btnView.Enabled = false;
btnExportExcel.Enabled = false;
btnPDF.Enabled = false;
break;
}
}
}
frmEmpRegister.cs
public partial class frmEmpRegister : Form
{
public frmEmpRegister()
{
InitializeComponent();
}
private void frmEmpRegister_Load(object sender, EventArgs e)
{
this.CenterToScreen();
switch (Role.UserRole.ToLower())
{
case "admin":
btnInsert.Enabled = true;
btnUpdate.Enabled = true;
btnDelete.Enabled = true;
btnView.Enabled = true;
btnExportExcel.Enabled = true;
btnPDF.Enabled = true;
break;
case "sky":
btnInsert.Enabled = true;
btnUpdate.Enabled = true;
btnDelete.Enabled = false;
btnView.Enabled = false;
btnExportExcel.Enabled = false;
btnPDF.Enabled = false;
break;
}
}
}
VB.Net
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
End Sub
Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
' Validate User and Assign role.
Role.UserRole = Me.txtRole.Text.Trim()
Dim mainForm As MainForm = New MainForm()
mainForm.Show()
End Sub
End Class
MainForm.vb
Public Class MainForm
Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
For Each item As ToolStripDropDownItem In toolStripMenuItem.DropDownItems
item.Enabled = False
Select Case Role.UserRole.ToLower()
Case "admin"
If item.Text = "Student Register" Then
item.Enabled = True
ElseIf item.Text = "Employee Register" Then
item.Enabled = True
End If
Case "abc"
If item.Text = "Student Register" Then
item.Enabled = True
ElseIf item.Text = "Employee Register" Then
item.Enabled = False
End If
Case "sky"
If item.Text = "Student Register" Then
item.Enabled = False
ElseIf item.Text = "Employee Register" Then
item.Enabled = True
End If
End Select
Next
End Sub
Private Sub studentRegisterToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles studentRegisterToolStripMenuItem.Click
Dim formStudent As frmStuRegister = New frmStuRegister()
formStudent.Show()
End Sub
Private Sub employeeRegisterToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles employeeRegisterToolStripMenuItem1.Click
Dim formEmployee As frmEmpRegister = New frmEmpRegister()
formEmployee.Show()
End Sub
End Class
frmStuRegister.vb
Public Class frmStuRegister
Private Sub frmStuRegister_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Select Case Role.UserRole.ToLower()
Case "admin"
btnInsert.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
btnView.Enabled = True
btnExportExcel.Enabled = True
btnPDF.Enabled = True
Case "abc"
btnInsert.Enabled = True
btnUpdate.Enabled = False
btnDelete.Enabled = False
btnView.Enabled = False
btnExportExcel.Enabled = False
btnPDF.Enabled = False
End Select
End Sub
End Class
frmEmpRegister.vb
Public Class frmEmpRegister
Private Sub frmEmpRegister_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.CenterToScreen()
Select Case Role.UserRole.ToLower()
Case "admin"
btnInsert.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
btnView.Enabled = True
btnExportExcel.Enabled = True
btnPDF.Enabled = True
Case "sky"
btnInsert.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = False
btnView.Enabled = False
btnExportExcel.Enabled = False
btnPDF.Enabled = False
End Select
End Sub
End Class
Screenshot