Loop through MenuItems

  • Thread starter Thread starter Steve Tan
  • Start date Start date
S

Steve Tan

1. How can I loop through all the items and subitems for Menu of a
Windows Form (c#) ? Recursive function?

2. How can I uniquely identify a submenu item ? MenuItems doesn't have
a Name property.


Thanks,
Steve
 
Steve Tan said:
1. How can I loop through all the items and subitems for Menu of a
Windows Form (c#) ? Recursive function?

That's one possibility. Send the MenuItems from your MainMenu to this
example:

private void menuItems(Menu.MenuItemCollection mc)
{
foreach (MenuItem m in mc)
{
// Do whatever you like with
// the "single" item...

if (m.IsParent)
{
menuItems(m.MenuItems);
}
}
}

2. How can I uniquely identify a submenu item ?
MenuItems doesn't have a Name property.

How you need to identify a submenu item is depending on what you need to do.
Each object can be identified by the value of it's reference.

If you have more than one item with the same value in the Text-property,
maybe it should behave similar anyway.

If you absolutely need to have a Name property you can easily subclass the
MenuItem-class to your own specialized class.

class NamedMenuItem : MenuItem
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}

public NamedMenuItem () : base()
{
this.name = this.Text;
}
}

// Bjorn A
 
Back
Top