Assign same Handler to more than one event?

Dinosaur

Member
Joined
Oct 30, 2007
Messages
11
Programming Experience
10+
I can code this by having two event handlers call the same subroutine.

It seems cleaner to have one Event Handler.

Can I do this at design time?

If not, during the Form Load Subroutine can I assign the same Event Handler to a second event?

I am doing geodesic (shortest distance) calculations for Ellipsoids of revolution & for Spheres. The same calculations can be used for both objects by merely using two equal Ellipsoid parameters.
 
If you use AddHandler and RemoveHandler, you have nothing special to do, just add the handler to the two events. If using Handles, separate the events with a comma.
 
I can code this by having two event handlers call the same subroutine.

It seems cleaner to have one Event Handler.

Can I do this at design time?
Yes you can. If you have an existing event handler then you can add another event to that handler in code yourself as Herman says, e.g. change this:
VB.NET:
Handles Button1.Click
to this
VB.NET:
Handles Button1.Click, Button2.Click
You can also do it in the designer window, whether or not you have an existing event handler. First select your multiple controls or components using Shift+Click+Drag or Ctrl+Click and open the Properties window and click the Events button. You then double-click the desired event and an event handler will be generated with multiple events in the Handles clause. You'll probably want to change the method name because it will be named after just one of the selected controls/components.

You can also add one or more events to the Handles clause of an existing event handler in the designer. Select one or more controls or components and then open the Properties window to display events. You can then select an existing event handler using the drop-down for the desired event. Only existing event handlers with a compatible signature are displayed. This also means that, not only can the controls and components be different types, the events being handled can be different types as well, e.g. you can handle the Click event of a Button and the SelectionChangeCommitted event of a ComboBox using the same event handler.
 
Back
Top