I have a ToolStripMenuItem
called myMenu
. How can I access this like so:
/* Normally, I would do: */
this.myMenu... etc.
/* But ho
Assuming you have the menuStrip
object and the menu is only one level deep, use:
ToolStripMenuItem item = menuStrip.Items
.OfType()
.SelectMany(it => it.DropDownItems.OfType())
.SingleOrDefault(n => n.Name == "MyMenu");
For deeper menu levels add more SelectMany operators in the statement.
if you want to search all menu items in the strip then use
ToolStripMenuItem item = menuStrip.Items
.Find("MyMenu",true)
.OfType()
.Single();
However, make sure each menu has a different name to avoid exception thrown by key duplicates.
To avoid exceptions you could use FirstOrDefault
instead of SingleOrDefault
/ Single
, or just return a sequence if you might have Name
duplicates.