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

Iterating through menustrip items

By , 3 May 2012
 

Introduction

It is quite difficult to set the properties of toolstrip menu items like if you want to disable certain items or make them invisible. It is simple to set them 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

The given code works on a 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 separator as well.
        {
            if (obj.GetType().Equals(typeof(ToolStripMenuItem)))
            //if we get the desired object type.
            {
                ToolStripMenuItem subMenu = (ToolStripMenuItem)obj;
                // cast to ToolStripMenuItem

                if (subMenu.HasDropDownItems) // if subMenu has children
                {
                    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 useful 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)

About the Author

Qadeer Ahmed Khan
Software Developer
Pakistan Pakistan
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membershanmugamvasu20 Oct '12 - 1:07 
QuestiontipmemberMrDeej3 May '12 - 8:27 
AnswerRe: tipmemberqadeer ahmed khan3 May '12 - 19:22 
GeneralRe: tipmemberSelvin3 May '12 - 23:41 
GeneralRe: tipmemberJohn Brett4 May '12 - 0:11 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130513.1 | Last Updated 4 May 2012
Article Copyright 2012 by Qadeer Ahmed Khan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid