Reputation: 121
For my current project i made a MDIform with "menuStrip" and a couple of "ToolStripMenuItem". a couple of buttons and a devexpress "NavbarControl"
The intention is that the user logs in with a userID the application will get a datarow for a specific "Control" in this row theirs a bool, if its true the Item must be visible, otherwise the item must be invisible.
the Datarow also contains the name of the item.
so i uses:
this.Controls[item].Visible = true;
item = string(name of item)
if i use this to hide the menustrip itself, it works if i try it on the MenuStipItems, it gives a null reference exception.
how can i control the items INSIDE the MenuStip, only by name of the item???
Code:
DataTable dt = GetData();
foreach (DataRow row in dt.Rows)
{
string item = row["ItemNaam"].ToString();
foreach (string rol in Rollen)
{
DataRow dr = GetDataByItemNaam(item);
if (Convert.ToBoolean(dr[rol]) == true)
{
this.Controls[item].Visible = true; //Show Item
}
}
}
Upvotes: 0
Views: 192
Reputation: 121
I've solved the problem:
I created a foreach
loop within a foreach
loop where
each loop looks for the name of the item, and then for the name of the item in the previous item.
If the name matches the given name, it sets the visibility to true.
This is for 2 levels, I created an additional two extra foreach
loops to go even deeper (inception) to 4 levels of items in the menu.
Perhaps its not the right/fastest way, but it works like it should.
Upvotes: 0
Reputation: 81620
The MenuStrip control has it's own collection. So to reference the menu strip items, reference the items from the menustrip parent:
if (this.menuStrip1.Items.ContainsKey(item))
this.menuStrip1.Items[item].Visible = true;
Upvotes: 1