Single event handler for menu

  • Thread starter Thread starter Martin
  • Start date Start date
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
 
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
 
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.
 
Back
Top