0

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
            }
        }
    }
0

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

oke, but a also had some buttons and items in a DevExpress NavBarControl that a want to hide the same way
@BramVdeventer Not familiar with the NavBarControl, but it would be the same concept. The items that you are trying to show and hide belong to a parent control, so you have to have a reference to that parent control.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.