Jean Paul's Blog

There are 2 types of People in the World, One who Likes SharePoint and..

    Advertisements
  • Microsoft MVP

  • MindCracker MVP

  • CodeProject MVP

  • eBook on SharePoint 2010

  • eBook on Design Patterns

  • eBook on Windows Azure

  • NLayers Framework @ CodePlex

  • MSDN Forums

  • .Net vs. Java

    Due to Public Demand
  • Advertisements

Posts Tagged ‘menu’

WinForms :: Get subitems in Menu programmatically

Posted by Paul on November 11, 2011

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)
MessageBox.Show(item.Text);
}

 

Advertisements

Posted in C# | Tagged: , , , , | Leave a Comment »