How can I get info about which MenuItem that is clicked at runtime?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M Shafaat

    How can I get info about which MenuItem that is clicked at runtime?

    Hi,
    I want to develop an application with menus. I want to make only one event
    handler method which handles a whole group of menu items by firstly
    identifying which MenuItem sends the click event and then performing the
    appropriate action. But I don't find any way to retrieve any info telling me
    which specific MenuItem sends a click event at the moment. The sender and e
    parameters seem to carry some very general information, not from a specific
    MenuItem sending the click event.


    This info must exist somewhere, because Windows knows which menu shall be
    performed at the moment., but C# seems to hide it for the programmer!


    Is there a way to get that info?


    Regards,
    M Shafaat



  • Jeff Louie

    #2
    Re: How can I get info about which MenuItem that is clicked at runtime?

    The standard way to respond to a menu is to register a separate event
    handler for each menu item, eliminating any switches, such that there is
    an event handler for each menu item. If you are dynamically adding menu
    items at runtime then this approach does not work. If you must add menu
    items dynamically at runtime you can declare a single event handler for
    all menu items in a menu so that each menu item in a given menu calls
    the same event handler.

    IEnumerator enumerator= collection.GetE numerator();
    while (enumerator.Mov eNext())
    {
    String menuName= (string)enumera tor.Current;
    menuItemShape.M enuItems.Add(me nuName,
    new EventHandler(th is.shape_Click) );
    }

    You can then act on the text of the menuitem at runtime in the single
    event handler.

    private void shape_Click(obj ect sender, System.EventArg s e)
    {
    currentShape= ((MenuItem)send er).Text;
    }

    Regards,
    Jeff

    *** Sent via Developersdex http://www.developersdex.com ***

    Comment

    • Bruce Wood

      #3
      Re: How can I get info about which MenuItem that is clicked at runtime?

      The sender parameter should be the menu item that generated the event.

      Comment

      Working...