By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,114 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,114 IT Pros & Developers. It's quick & easy.

Loop through MenuItems

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Steve Tan" <st********@yahoo.com> skrev i meddelandet
news:95**************************@posting.google.c om...
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

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.