How can i reference a MenuItem?

165 views Asked by At

I am adding MenuItems to my ContextMenu like this:

mnuContextMenu.MenuItems.Add("Delete", DeleteFile);

Now i want to disable this MenuItem, like this:

x.Enabled = false;

What MenuItem reference do I have to use for x?

1

There are 1 answers

0
Tim On BEST ANSWER

You don't have anything to directly reference it. You could get it using the indexer of the MenuItems property:

mnuContextMenu.MenuItems[0].Enabled = false; // if it were the first item

or you could have a reference when creating it:

var deleteMenuItem = new MenuItem("Delete", DeleteFile);
mnuContextMenu.MenuItems.Add(deleteMenuItem);

and then you have the reference to use later:

deleteMenuItem.Enabled = false;

You may need to store it as a private data member of your class, rather than a local variable if you're planning to use it outside of your current function.