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
}
}
}
I’ve solved the problem:
I created a
foreachloop within aforeachloop whereeach 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
foreachloops 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.