Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void itemcount()
       {

           //for (int i = 0; i < this.menuStrip.Items.Count; i++)
           //{
           //    //main menus items
           //    string mnuItemName = this.menuStrip.Items[i].Name;
           //    ToolStripMenuItem tmpMenuItem = this.menuStrip.Items[i] as ToolStripMenuItem;

           //    //check for sub menu
           //    for (int j = 0; j < tmpMenuItem.DropDownItems.Count; j++)
           //    {
           //        string menuSubItem = tmpMenuItem.DropDownItems[j].Name;
           //        MessageBox.Show(menuSubItem);
           //    }
           //}

           foreach (ToolStripMenuItem masterToolStripMenuItem in this.menuStrip.Items)
           {

               foreach (ToolStripMenuItem master in this.masterToolStripMenuItem.DropDownItems)
               {//mnu.Text  = main item

                   //You can just count:
                   int @int = masterToolStripMenuItem.DropDownItems.Count;
                   MessageBox.Show(@int.ToString());
                   break;

                   //Or Loop perhaps to get even more if needed
                   //foreach (ToolStripMenuItem m in master.DropDownItems)
                   //{

                   //    //Drop Down items
                   //}
               }
           }
       }
Posted

1 solution

You can use the How to iterate recursive through all menu items in a menuStrip Control[^] Tip to calculate Items count easily.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900