Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass an event to a method and then subscribe to it?

Event Handler

public void DeliverEvent(object sender, EventArgs e)
{

}

#1: This Works

public void StartListening(Button source)
{
    source.Click += DeliverEvent;
}

#2: And so does this..

public void StartListening(EventHandler eventHandler)
{
    eventHandler += DeliverEvent;
}

But in #2, you cannot call the method because if you try something like this:

StartListening(button.Click);

You get this error:

The event 'System.Windows.Forms.Control.Click' can only appear on the left hand side of += or -=

Is there any way around that error? I want to be able to pass the event and not the object housing the event to the StartListening method.

like image 590
Ryan Peschel Avatar asked Jan 18 '26 06:01

Ryan Peschel


2 Answers

You are mixing events with delegates. You should probably read this article by Jon Skeet which will explain the differences between the two.

The reason the second option doesn't work is because the method expects a parameter that is a delegate that conforms to the EventHandler signature.Button.Click refers to an event rather than a delegate. Events just encapsulate delegates.

I'm not sure what you are trying can be done. Conceptually what you want to do doesn't actually make sense; it is the logic of the handler you want to pass around not the event itself.

Take a look at this post which looks at some ways of simulating the effect you want.

like image 150
Benjamin Gale Avatar answered Jan 20 '26 18:01

Benjamin Gale


The Microsoft Reactive Extensions or Rx framework provides a method to turn an event into an observable. Events cannot be passed around as parameters, but observables can. When observables are subscribed to they set up handlers for the underlying event.

The signature (of one of the overloads) looks like this:

IObservable<EventPattern<TEventArgs>> FromEventPattern<TDelegate, TEventArgs>(
        Action<TDelegate> addHandler, Action<TDelegate> removeHandler
    ) where TEventArgs: EventArgs

For example, to define a MouseMove observable from an event this code can be used:

        IObservable<EventPattern<MouseEventArgs>> mouseMoves =
            Observable
                .FromEventPattern<MouseEventHandler, MouseEventArgs>(
                    h => richTextBox1.MouseMove += h,
                    h => richTextBox1.MouseMove -= h);

You can then pass around a reference to IObservable<EventPattern<MouseEventArgs>> mouseMoves and subscribe to it like so:

        var subscription = 
            mouseMoves
                .Subscribe(ep =>
                {
                    var x = ep.EventArgs.X;
                    var y = ep.EventArgs.Y;
                    // etc
                });

Detaching from the observable/event becomes as simple as calling this:

        subscription.Dispose();

You can get Rx via Nuget.

like image 39
Enigmativity Avatar answered Jan 20 '26 20:01

Enigmativity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!