Click here to Skip to main content
Click here to Skip to main content

How to iterate recursive through all menu items in a menuStrip Control

, 7 Oct 2011
Rate this:
Please Sign up or sign in to vote.
How to iterate recursively through all menu items in a menuStrip Control
For a multilanguage project last time i faced the problem how to translate the menu entries of a menuStrip Control and enable/disable the controls for security settings.
 
So i created a small helper class to get a list of all entries and i like to share with u the solution.
 
Basically it's a simple recursive iteration;
So here we go:
 
namespace Test_MenuItemIteration
{
    using System.Collections.Generic;
    using System.Windows.Forms;
 
    public static class GetAllMenuStripItems
    {
        #region Methods
 
        /// <summary>
        /// Gets a list of all ToolStripMenuItems
        /// </summary>
        /// <param name="menuStrip">The menu strip control</param>
        /// <returns>List of all ToolStripMenuItems</returns>
        public static List<ToolStripMenuItem> GetItems(MenuStrip menuStrip)
        {
            List<ToolStripMenuItem> myItems = new List<ToolStripMenuItem>();
            foreach (ToolStripMenuItem i in menuStrip.Items)
            {
                GetMenuItems(i, myItems);
            }
            return myItems;
        }
 
        /// <summary>
        /// Gets the menu items.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="items">The items.</param>
        private static void GetMenuItems(ToolStripMenuItem item, List<ToolStripMenuItem> items)
        {
            items.Add(item);
            foreach (ToolStripItem i in item.DropDownItems)
            {
                if (i is ToolStripMenuItem)
                {
                    GetMenuItems((ToolStripMenuItem)i, items);
                }
            }
        }
 
        #endregion Methods
    }
}
 

[GetItems] iterates through the TopLevel menus and call [GetMenuItems]
GetMenuItems checks whether DropDwonItems exists or not.
If yes, it will call itself to find all levels of menu entries.
 
How to use the code in your App:
 
List<ToolStripMenuItem> myItems = GetAllMenuStripItems.GetItems(this.menuStrip1);
foreach (var item in myItems)
{
    MessageBox.Show(item.Text);
}
 
Hope the snippet is usefull and easy to understand.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Stefan Huy
Engineer
Germany Germany
No Biography provided
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionHow to make recursive menu in Asp.Net c# Pinmemberberkut200619-Jun-13 14:38 
BugMenu item type other than ToolStripMenuItem will throw an exception PinmemberGaia Defender6-Dec-12 10:01 
GeneralCool PinmemberShahan Ayyub28-Apr-12 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 7 Oct 2011
Article Copyright 2011 by Stefan Huy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid