Loop through MenuItems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Tan

    #1

    Loop through MenuItems

    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
  • Bjorn Abelli

    #2
    Re: Loop through MenuItems


    "Steve Tan" <stevetan76@yah oo.com> skrev i meddelandet
    news:95a498a0.0 311160107.3aa93 b18@posting.goo gle.com...[color=blue]
    > 1. How can I loop through all the items and subitems for Menu of a
    > Windows Form (c#) ? Recursive function?[/color]

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

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

    if (m.IsParent)
    {
    menuItems(m.Men uItems);
    }
    }
    }

    [color=blue]
    > 2. How can I uniquely identify a submenu item ?[/color]
    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



    Comment

    Working...