|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Iterating__MenuStrip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
#region Load Rights
//Transaction Forms
UserRights.Add(new UserRight() { FormID = 1, FormName = "Create Employee", CanAccess = false });
UserRights.Add(new UserRight() { FormID = 2, FormName = "Create Customer", CanAccess = true });
//Report Forms
UserRights.Add(new UserRight() { FormID = 3, FormName = "Employee Report", CanAccess = false });
UserRights.Add(new UserRight() { FormID = 4, FormName = "Customer Report", CanAccess = true });
#endregion
#region Set Rights
foreach (ToolStripMenuItem mainMenu in ts.Items)
{
// navigate through each submenu
SetToolStripItems(mainMenu.DropDownItems);
}
#endregion
}
private void SetToolStripItems(ToolStripItemCollection dropDownItems)
{
try
{
foreach (object obj in dropDownItems)
//for each object.
{
ToolStripMenuItem subMenu = obj as ToolStripMenuItem;
//Try cast to ToolStripMenuItem as it could be toolstrip separator as well.
if (subMenu != null)
//if we get the desired object type.
{
if (subMenu.HasDropDownItems) // if subMenu has children
{
SetToolStripItems(subMenu.DropDownItems); // Call recursive Method.
}
else // Do the desired operations here.
{
if (subMenu.Tag != null)
{
subMenu.Visible = UserRights.Where(x => x.FormID == Convert.ToInt32(subMenu.Tag)).First().CanAccess;
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SetToolStripItems",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#region Rights List to be loaded from Database.
private List<UserRight> UserRights = new List<UserRight>();
#endregion
#region Dialog Messages
private void createEmployeeToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Create Employee");
}
private void createCustomerToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Create Customer");
}
private void employeeReportToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Employee Report");
}
private void customerReportToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Customer Report");
}
#endregion
}
public class UserRight
{
public int FormID;
public string FormName;
public bool CanAccess;
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.