Click here to Skip to main content
13,357,219 members (61,982 online)
Click here to Skip to main content
Add your own
alternative version


8 bookmarked
Posted 2 May 2012

Iterating through Menu strip Items.

, 2 May 2012
Rate this:
Please Sign up or sign in to vote.
This is an old version of the currently published tip/trick.


Its quite difficult to set properties of Tool Strip Menu Items like if you want to disable curtain items or make then invisible. Simple why is to set then individually but if you want to iterate through all the items it's quite difficult this article explains how you can iterate through all ToolStripMenuItems of a menu strip. 


Given code works on recursive method call.

Using the Code

// if we have a MenuStrip named ts.

   foreach (ToolStripMenuItem mainMenu in ts.Items)
                if (mainMenu.Visible)
                    // navigate through each submenu
        private void  SetToolStripItems(ToolStripItemCollection dropDownItems)
                foreach (object obj in dropDownItems) //for each object because it could be toolstrip seperator as well.
                    if (obj.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") //if we get the desired object type.
                        ToolStripMenuItem subMenu = (ToolStripMenuItem)obj; // cast to ToolStripMenuItem

                        if (subMenu.HasDropDownItems) // if subMenu has childrens
                            SetToolStripItems(subMenu.DropDownItems); // Call recursive Method.
                        else // Do the desired operations here.
                            if (subMenu.Tag != null)
                                subMenu.Visible = AccountsGlobals.FormRights(Convert.ToInt32(subMenu.Tag)).CanAccess;
                                subMenu.Enabled  = AccountsGlobals.FormRights(Convert.ToInt32(subMenu.Tag)).CanAccess;
            catch (Exception ex)
                MessageBox.Show(ex.Message, "SetToolStripItems", MessageBoxButtons.OK, MessageBoxIcon.Error);

Points of Interest

Recursive methods are quite use full when you don't know the depth of the loops.


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


About the Author

Qadeer Ahmed Khan
Software Developer
Pakistan Pakistan
No Biography provided

You may also be interested in...


Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
QuestionToolStripDropDownItem Pin
Jens-Madsen26-Oct-14 18:29
memberJens-Madsen26-Oct-14 18:29 
GeneralMy vote of 5 Pin
shanmugamvasu20-Oct-12 2:07
membershanmugamvasu20-Oct-12 2:07 
Questiontip Pin
MrDeej3-May-12 9:27
memberMrDeej3-May-12 9:27 
AnswerRe: tip Pin
qadeer ahmed khan3-May-12 20:22
memberqadeer ahmed khan3-May-12 20:22 
GeneralRe: tip Pin
Selvin4-May-12 0:41
memberSelvin4-May-12 0:41 
GeneralRe: tip Pin
John Brett4-May-12 1:11
memberJohn Brett4-May-12 1:11 

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
Web03 | 2.8.180111.1 | Last Updated 2 May 2012
Article Copyright 2012 by Qadeer Ahmed Khan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid