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

Iterating through Menu strip Items.

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

Introduction 

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. 

Background

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
                    SetToolStripItems(mainMenu.DropDownItems);
                }               
            }
        private void  SetToolStripItems(ToolStripItemCollection dropDownItems)
        {
            try
            {
                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.

License

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

Share

About the Author

Qadeer Ahmed Khan
Software Developer
Pakistan Pakistan
No Biography provided

Comments and Discussions


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
GeneralMy vote of 5 Pinmembershanmugamvasu20-Oct-12 1:07 
Questiontip PinmemberMrDeej3-May-12 8:27 
AnswerRe: tip Pinmemberqadeer ahmed khan3-May-12 19:22 
GeneralRe: tip PinmemberSelvin3-May-12 23:41 
GeneralRe: tip PinmemberJohn Brett4-May-12 0:11 

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
Web04 | 2.8.141022.2 | Last Updated 2 May 2012
Article Copyright 2012 by Qadeer Ahmed Khan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid