Single event handler for menu

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

    #1

    Single event handler for menu

    I have a menu, a rather big one. It contains about 150 items. Normally this
    would mean that I'd have about 150 event handlers:


    Private Sub Menu1_Click(ByV al sender As System.Object, ByVal e As
    System.EventArg s) Handles Menu1.Click

    However, I would like to get the associated form name from a different
    source and then create a single event handler:

    Private Sub OpenForm (ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles AllMenus.Click

    In VB2005 I can assign multiple events to a single handler by specifying
    them in the 'Handles' part, but for 150 items I think this is a bit much. Is
    there another way to specify a handler to menu items? Like looping through
    the menus and assign a specific handler?

    Tia,
    Martin


  • Larry Lard

    #2
    Re: Single event handler for menu


    Martin wrote:[color=blue]
    > I have a menu, a rather big one. It contains about 150 items. Normally this
    > would mean that I'd have about 150 event handlers:
    >
    >
    > Private Sub Menu1_Click(ByV al sender As System.Object, ByVal e As
    > System.EventArg s) Handles Menu1.Click
    >
    > However, I would like to get the associated form name from a different
    > source and then create a single event handler:
    >
    > Private Sub OpenForm (ByVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles AllMenus.Click
    >
    > In VB2005 I can assign multiple events to a single handler by specifying
    > them in the 'Handles' part, but for 150 items I think this is a bit much. Is
    > there another way to specify a handler to menu items? Like looping through
    > the menus and assign a specific handler?[/color]

    Yes. With OpenForm defined as above (without a Handles clause)

    ' "for each menu item" code omitted
    ' as this depends on how you build / define your menus

    AddHandler theMenuItem.Cli ck, AddressOf OpenForm

    ' if you build menu dynamically, do this just after creation
    ' for convenience

    --
    Larry Lard
    Replies to group please

    Comment

    • Chris Dunaway

      #3
      Re: Single event handler for menu

      Yes, check out the AddHandler statement.

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Single event handler for menu

        "Martin" <x@y.com> schrieb:[color=blue]
        > In VB2005 I can assign multiple events to a single handler by specifying
        > them in the 'Handles' part, but for 150 items I think this is a bit much.
        > Is there another way to specify a handler to menu items? Like looping
        > through the menus and assign a specific handler?[/color]

        IIRC 'MenuItem' has an overloaded constructor which accepts the 'Click'
        event handler. However, I fear that this constructor cannot be used from
        within the Windows Forms designer.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Martin

          #5
          Re: Single event handler for menu

          Hi Larry,

          Thanks, that did the trick. I appreciate it.

          Martin

          "Larry Lard" <larrylard@hotm ail.com> wrote in message
          news:1141913751 .942359.130920@ e56g2000cwe.goo glegroups.com.. .[color=blue]
          >
          > Martin wrote:[color=green]
          >> I have a menu, a rather big one. It contains about 150 items. Normally
          >> this
          >> would mean that I'd have about 150 event handlers:
          >>
          >>
          >> Private Sub Menu1_Click(ByV al sender As System.Object, ByVal e As
          >> System.EventArg s) Handles Menu1.Click
          >>
          >> However, I would like to get the associated form name from a different
          >> source and then create a single event handler:
          >>
          >> Private Sub OpenForm (ByVal sender As System.Object, ByVal e As
          >> System.EventArg s) Handles AllMenus.Click
          >>
          >> In VB2005 I can assign multiple events to a single handler by specifying
          >> them in the 'Handles' part, but for 150 items I think this is a bit much.
          >> Is
          >> there another way to specify a handler to menu items? Like looping
          >> through
          >> the menus and assign a specific handler?[/color]
          >
          > Yes. With OpenForm defined as above (without a Handles clause)
          >
          > ' "for each menu item" code omitted
          > ' as this depends on how you build / define your menus
          >
          > AddHandler theMenuItem.Cli ck, AddressOf OpenForm
          >
          > ' if you build menu dynamically, do this just after creation
          > ' for convenience
          >
          > --
          > Larry Lard
          > Replies to group please
          >[/color]


          Comment

          • Martin

            #6
            Re: Single event handler for menu

            Thanks for your help.

            Martin

            "Martin" <x@y.com> wrote in message
            news:%233QGSx3Q GHA.5036@TK2MSF TNGP12.phx.gbl. ..[color=blue]
            >I have a menu, a rather big one. It contains about 150 items. Normally this
            >would mean that I'd have about 150 event handlers:
            >
            >
            > Private Sub Menu1_Click(ByV al sender As System.Object, ByVal e As
            > System.EventArg s) Handles Menu1.Click
            >
            > However, I would like to get the associated form name from a different
            > source and then create a single event handler:
            >
            > Private Sub OpenForm (ByVal sender As System.Object, ByVal e As
            > System.EventArg s) Handles AllMenus.Click
            >
            > In VB2005 I can assign multiple events to a single handler by specifying
            > them in the 'Handles' part, but for 150 items I think this is a bit much.
            > Is there another way to specify a handler to menu items? Like looping
            > through the menus and assign a specific handler?
            >
            > Tia,
            > Martin
            >[/color]


            Comment

            Working...