private void SetUserTasks() { try { DAL.UserTasks objDAL = new DAL.UserTasks(); objDAL.connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["MyString"].ConnectionString; DataTable dt = objDAL.getRecord(vUserID); menuStrip.Items.OfType <ToolStripItem>().ToList().ForEach(item => { if (item.Name != "mnuRegistration" && item.Name != "mnuFinance" && item.Name != "mnuReports" && item.Name != "mnuTools" && item.Name != "mnuHelp") { var isAllow = dt.Select(" Selected=1 AND TaskKey='" + item.Name + "'"); if (isAllow.Length > 0) { item.Visible = true; } else { item.Visible = false; } } }); } catch (Exception exc) { throw; } }
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.Name != null) { if (subMenu.Name != "exitToolStripMenuItem") { DAL.UserTasks objDAL = new DAL.UserTasks(); objDAL.connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["MyString"].ConnectionString; DataTable dt = objDAL.getRecord(vUserID); var isAllow = dt.Select(" Selected=1 AND TaskKey='" + subMenu.Name + "'"); if (isAllow.Length > 0) { subMenu.Visible = true; } else { subMenu.Visible = false; } } } } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "SetToolStripItems", MessageBoxButtons.OK, MessageBoxIcon.Error); } }