Dilhan
Dilhan

Reputation: 1

How to print names of all menuitems in C#?

I am new to programming. I need to get a list of ALL menuItems (ToolStripMenuItems) including Dropdown menuitems. I found some codes, but it list Main Menu Items only, No Dropdown menuItems. Can you give me a suitable code for list ALL menuItems.

    foreach (ToolStripMenuItem item in menuStrip.Items)
    {
         MessageBox.Show(item.Name);
    }

Upvotes: 0

Views: 1157

Answers (1)

Uwe Keim
Uwe Keim

Reputation: 40736

The child items have a DropDownItems collection.

Write a recursive function like this one:

private void print( ToolStripMenuItem element )
{
    MessageBox.Show(element.Name);

    foreach ( ToolStripMenuItem child in element.DropDownItems )
    { 
        print( child );
    }
}

Upvotes: 3

Related Questions