Single event handler for menu

M

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
 
L

Larry Lard

Martin said:
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
 
H

Herfried K. Wagner [MVP]

Martin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top