Click here to Skip to main content
15,884,058 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am trying to access the Level2 and Level 3 Menu Item of the MDI form how can I do that??

Here is my Menu Structure
MAIN MENU 
              SUB MENU -> Level 2 Menu1
         SUB MENU 2  -> Level 2 Menu1 --> Level3 Menu1



I want to get Menu text of Sub Menu -> Level 2 Menu -> Level 2 Menu1 -> Level 3 Menu

I have tried to iterate through all the MenuStrip menu, ToolStripMenuItem and ToolStrip Items but I can only get the menus till SubMenu I cant reach till Level2 or Level2 Menu. Below is the code I have used

C#
foreach (Control ctrl in coll)
{
   MenuStrip menustrip = ctrl as MenuStrip;

   if (ctrl.Text == "MenuStrip")
   {
      foreach (ToolStripMenuItem toolstripmenuitem in menustrip.Items)
      {
         foreach (ToolStripItem toolstripmenuitem in toolscriptmenuitem.DropDownItems)
         {
            MessageBox.Show(toolstripmenuitem.Text);
         }
      }
   }
}


I am using C# Windows Form Application MDI Form.

Please help.

Thanks Ankit Roy
Posted
Updated 18-Apr-15 12:07pm
v2

1 solution

You need to use recursion here:
C#
public void GetMenuItems(ToolStripItemCollection menuItems)
{
    foreach (ToolStripMenuItem itm in menuItems)
    {
        Console.WriteLine(itm.Text);

        if (itm.HasDropDown)
        {
            GetMenuItems(itm.DropDownItems);
        }
    }
}
 
Share this answer
 
Comments
Ankit Roy 24-Apr-15 4:28am    
Thanks Bill
But I still not able to get the menu Items. Can explain little more.

Thanks

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