Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get an actual EventHandler delegate instance from an event in VB.NET?

In C#, I could do something like this:

EventHandler handler = this.SomeEvent;

...which would allow me to, for example, do:

Delegate[] attachedHandlers = handler.GetInvocationList();

In VB.NET, I can't seem to figure out how to do a similar thing.

This doesn't work:

Dim handler As EventHandler = Me.SomeEvent

...due to the following error:

Public Event SomeEvent(sender As Object, e As EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

But this doesn't work either:

Dim handler As EventHandler = AddressOf Me.SomeEvent

...because:

'AddressOf' operand must be the name of a method (without parentheses).

So how can I actually get an EventHandler from an event in VB.NET? The only idea that's immediately coming to mind is to use reflection, but that seems pretty ridiculous.

like image 371
Dan Tao Avatar asked Nov 05 '10 16:11

Dan Tao


People also ask

Is an EventHandler a delegate?

The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.

Can event handler return a value?

The standard signature of an event handler delegate defines a method that does not return a value. This method's first parameter is of type Object and refers to the instance that raises the event. Its second parameter is derived from type EventArgs and holds the event data.

What is the EventHandler class in terms of event driven programming?

In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.

How do you pass an event handler as a parameter?

If you want to pass a parameter to the click event handler you need to make use of the arrow function or bind the function. If you pass the argument directly the onClick function would be called automatically even before pressing the button.


1 Answers

   Private Event MyEvent()
   Private delegates() As System.Delegate = MyEventEvent.GetInvocationList()

undocumented, found here

like image 131
Tim Schmelter Avatar answered Oct 20 '22 18:10

Tim Schmelter