Do you have C# sample code on how to enumerate Menu Items?

Class library usage, coding and language questions.
kosorok
Posts: 1
Joined: Thu Sep 07, 2006 2:44 am

Do you have C# sample code on how to enumerate Menu Items?

Post by kosorok » Thu Sep 07, 2006 2:54 am

I've been able to find the MenuBar item in our application, but I'd like to see if you have any C# sample code on how to enumerate through the object to get all the available menu items, then be able to click on the desired menu item. For example, I can find the menu bar with File | Help on it, then I'd like to be able to select Help | About, or File | Open...

admin
Site Admin
Site Admin
Posts: 350
Joined: Wed Jul 05, 2006 7:44 pm

Post by admin » Thu Sep 07, 2006 7:48 pm

The following code sample dumps out all submenus and all submenu items of a menu bar.

Code: Select all

Int32 submenuCount = menuStrip.GetItemCount();
for (int position = 1; position <= submenuCount; position++)
{
    String submenuText = menuStrip.GetItemText(position);
    Console.WriteLine("  Submenu({0})={1}", position, submenuText);

    Int32 itemCount = menuStrip.GetItemCount(position);
    for (int itemPosition = 1; itemPosition <= itemCount; itemPosition++)
    {
        itemText = menuStrip.GetItemText(position,itemPosition);
        Console.WriteLine("    Item({0})={1}", itemPosition, itemText);
    }
}
You can select a menu item as follows:

Code: Select all

// Select the third menu item of the first submenu
menuStrip.SelectPosition(1,3);

// Select the Open menu item of the File submenu
menuStrip.SelectItemText("File","Open");
Jenö Herget
Ranorex Team