Events with Programmatically-Created Context Menu Items

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt Harvey

    #1

    Events with Programmatically-Created Context Menu Items

    Let's say that I populate a context menu with commands from a database
    table using a loop, and that these commands and their number can change
    at any time. Is there a way to dynamically have events to fire when
    clicking one of the items, using generic code?

    For instance, let's say that these are the items on my context menu from
    the database:

    COMMAND1
    COMMAND2
    COMMAND3

    Now let's say that I haven't actually coded a click event for any of
    these, but want to use a generic click event so that if I click
    "COMMAND3", the menuitem that's been clicked is passed to a function
    containing a switch or something like that.

    Is this possible?

  • Peter Bromberg [C# MVP]

    #2
    RE: Events with Programmaticall y-Created Context Menu Items

    You can certainly use the same "Generic" eventhandler for all of them, and
    inside its code you can determine "which one" it was that sent the event via
    the "sender" argument.

    Peter
    --
    Co-founder, Eggheadcafe.com developer portal:

    UnBlog:





    "Matt Harvey" wrote:
    [color=blue]
    > Let's say that I populate a context menu with commands from a database
    > table using a loop, and that these commands and their number can change
    > at any time. Is there a way to dynamically have events to fire when
    > clicking one of the items, using generic code?
    >
    > For instance, let's say that these are the items on my context menu from
    > the database:
    >
    > COMMAND1
    > COMMAND2
    > COMMAND3
    >
    > Now let's say that I haven't actually coded a click event for any of
    > these, but want to use a generic click event so that if I click
    > "COMMAND3", the menuitem that's been clicked is passed to a function
    > containing a switch or something like that.
    >
    > Is this possible?
    >
    >[/color]

    Comment

    • Matt Harvey

      #3
      Re: Events with Programmaticall y-Created Context Menu Items

      Peter Bromberg [C# MVP] wrote:[color=blue]
      > You can certainly use the same "Generic" eventhandler for all of them, and
      > inside its code you can determine "which one" it was that sent the event via
      > the "sender" argument.
      >
      > Peter[/color]
      Can anyone point me to a resource as to how to do this?

      Thanks,
      Matt

      Comment

      • Stoitcho Goutsev \(100\)

        #4
        Re: Events with Programmaticall y-Created Context Menu Items

        Matt,

        No, there is no generic event for the menu items.

        What Peter suggested is to add event handlers to all menu items upon
        creation. All the delegates will point to the same method. Inside the method
        you can use the *sender* parameter to determine the menu item that was
        clicked.


        If there is something not clear don't hesitate to post it.


        --
        HTH
        Stoitcho Goutsev (100)

        "Matt Harvey" <rifleman@gmail .com> wrote in message
        news:ujCc$lzMGH A.2416@TK2MSFTN GP15.phx.gbl...[color=blue]
        > Peter Bromberg [C# MVP] wrote:[color=green]
        >> You can certainly use the same "Generic" eventhandler for all of them,
        >> and inside its code you can determine "which one" it was that sent the
        >> event via the "sender" argument.
        >>
        >> Peter[/color]
        > Can anyone point me to a resource as to how to do this?
        >
        > Thanks,
        > Matt[/color]


        Comment

        Working...