WinForms :: Get subitems in Menu programmatically

This is a small piece of code to iterate over menu sub items dynamically. The topic was generated from a forum fix and I thought it will be a good idea to blog it.

/* Place a menu strip control with File and Edit as the top level items.  Inside the File menu add 3 subitems.  Those items can be viewed dynamically using the following code */


MenuStrip menuStrip = this.Controls.Cast<Control>().Where(c => (c is MenuStrip)).FirstOrDefault() as MenuStrip;
if (menuStrip != null)
ToolStripMenuItem menuItem = menuStrip.Items.Cast<ToolStripMenuItem>().Where(mi => mi.Text.Replace("&", string.Empty).ToLower() == "file").FirstOrDefault() as ToolStripMenuItem;
foreach (ToolStripItem item in menuItem.DropDown.Items)


Leave a Reply

Your email address will not be published. Required fields are marked *