Click here to Skip to main content
15,891,431 members
Articles / Programming Languages / C#

Iterating through menustrip Items

Rate me:
Please Sign up or sign in to vote.
4.75/5 (6 votes)
3 May 2012CPOL 54.3K   581   8  
This tip explains how you can iterate through all ToolStripMenuItems of a menu strip.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Iterating__MenuStrip
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            #region Load Rights

            //Transaction Forms
            UserRights.Add(new UserRight() { FormID = 1, FormName = "Create Employee", CanAccess = false  });
            UserRights.Add(new UserRight() { FormID = 2, FormName = "Create Customer", CanAccess = true });


            //Report Forms
            UserRights.Add(new UserRight() { FormID = 3, FormName = "Employee Report", CanAccess = false });
            UserRights.Add(new UserRight() { FormID = 4, FormName = "Customer Report", CanAccess = true });

            #endregion

            #region Set Rights

            foreach (ToolStripMenuItem mainMenu in ts.Items)
            {
                // navigate through each submenu
                SetToolStripItems(mainMenu.DropDownItems);
            }

            #endregion
        }


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

        #region Rights List to be loaded from Database.

        private List<UserRight> UserRights = new List<UserRight>();

        #endregion

        #region Dialog Messages

        private void createEmployeeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Create Employee");
        }

        private void createCustomerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Create Customer");
        }

        private void employeeReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Employee Report");
        }

        private void customerReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Customer Report");
        }

        #endregion
    }


    public class UserRight
    {
        public int FormID;
        public string FormName;
        public bool CanAccess;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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