Click here to Skip to main content
13,549,145 members
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 6 Oct 2011
Licenced CPOL

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)
            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)

Hope the snippet is usefull and easy to understand.


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


About the Author

Stefan Huy
Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThank You Pin
PasinduM9-Jun-15 20:08
memberPasinduM9-Jun-15 20:08 
BugMenu item type other than ToolStripMenuItem will throw an exception Pin
Gaia Defender6-Dec-12 10:01
memberGaia Defender6-Dec-12 10:01 
GeneralCool Pin
Shahan Ayyub28-Apr-12 23:43
memberShahan Ayyub28-Apr-12 23:43 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04-2016 | 2.8.180515.1 | Last Updated 7 Oct 2011
Article Copyright 2011 by Stefan Huy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid