Click here to Skip to main content
15,867,330 members
Articles / Programming Languages / C#
Tip/Trick

Iterating through menustrip Items

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
3 May 2012CPOL 53.8K   579   8   6
This tip explains how you can iterate through all ToolStripMenuItems of a menu strip.

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

C#
private void SetToolStripItems(ToolStripItemCollection dropDownItems)
        {
            try
            {
                foreach (object obj in dropDownItems)
                //for each object.
                {
                    ToolStripMenuItem subMenu = obj as ToolStripMenuItem;
                    //Try cast to ToolStripMenuItem as it could be toolstrip separator as well.

                    if (subMenu != null)
                    //if we get the desired object type.
                    {
                        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 = UserRights.Where(x => 
                                x.FormID == Convert.ToInt32(subMenu.Tag)).First().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)


Written By
Software Developer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionToolStripDropDownItem Pin
Jens Madsen, Højby26-Oct-14 17:29
Jens Madsen, Højby26-Oct-14 17:29 
Test for 'is
VB
ToolStripDropDownItem
' instead of ToolStripMenuItem to
cover ToolStripMenuItem, ToolStripDropDownButton and ToolStripSplitButton in one; cast the item to
VB
ToolStripDropDownItem
to get the .DropDownItems' Collection. Smile | :)
GeneralMy vote of 5 Pin
Shanmugam Vasu20-Oct-12 1:07
Shanmugam Vasu20-Oct-12 1:07 
Questiontip Pin
MrDeej3-May-12 8:27
MrDeej3-May-12 8:27 
AnswerRe: tip Pin
Qadeer Ahmed Khan3-May-12 19:22
Qadeer Ahmed Khan3-May-12 19:22 
GeneralRe: tip Pin
Selvin3-May-12 23:41
Selvin3-May-12 23:41 
GeneralRe: tip Pin
John Brett4-May-12 0:11
John Brett4-May-12 0: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.