Home All Groups Group Topic Archive Search About

Single event handler for menu

Author
9 Mar 2006 1:21 PM
Martin
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(ByVal sender As System.Object, ByVal e As
System.EventArgs) 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.EventArgs) 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

Author
9 Mar 2006 2:15 PM
Larry Lard
Martin wrote:
Show quoteHide quote
> 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(ByVal sender As System.Object, ByVal e As
> System.EventArgs) 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.EventArgs) 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?

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.Click, AddressOf OpenForm

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

--
Larry Lard
Replies to group please
Author
10 Mar 2006 12:58 PM
Martin
Hi Larry,

Thanks, that did the trick. I appreciate it.

Martin

Show quoteHide quote
"Larry Lard" <larryl***@hotmail.com> wrote in message
news:1141913751.942359.130920@e56g2000cwe.googlegroups.com...
>
> Martin wrote:
>> 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(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) 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.EventArgs) 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?
>
> 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.Click, AddressOf OpenForm
>
> ' if you build menu dynamically, do this just after creation
> ' for convenience
>
> --
> Larry Lard
> Replies to group please
>
Author
9 Mar 2006 2:17 PM
Chris Dunaway
Yes, check out the AddHandler statement.
Author
9 Mar 2006 2:39 PM
Herfried K. Wagner [MVP]
"Martin" <x@y.com> schrieb:
> 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?

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/>
Author
10 Mar 2006 12:58 PM
Martin
Thanks for your help.

Martin

Show quoteHide quote
"Martin" <x@y.com> wrote in message
news:%233QGSx3QGHA.5036@TK2MSFTNGP12.phx.gbl...
>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(ByVal sender As System.Object, ByVal e As
> System.EventArgs) 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.EventArgs) 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
>