0

How can i get the event handler attached to an event?

pseudocode, for example's sake:

public static EventHandler GetChangeUICuesEventHandler(Control SomeControl)
{
   EventHandler changeUICuesEventHandler = SomeControl.ChangeUICues;
   return changeUICuesEventHandler
}

Sample usage (pseudo-code):

Control control = GetControlByName(Console.ReadLine());
button1.ChangeUICues += GetChangeUICuesEventHandler(control);
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • In your pseudocode method at the start, I would expect there could be multiple event handlers attached to the event, so you'll need to return an IEnumerable if you can get this to work. – Reddog Nov 17 '11 at 21:02
  • I'm not sure you can do that. Usually I let each control implement an interface where the event is defined. If that is not possible I usually chain events. – Albin Sunnanbo Nov 17 '11 at 21:10
  • possible duplicate of [How to determine if an event is already subscribed](http://stackoverflow.com/questions/2697247/how-to-determine-if-an-event-is-already-subscribed) – Hans Passant Nov 17 '11 at 22:15
  • http://stackoverflow.com/questions/293007/is-it-possible-to-steal-an-event-handler-from-one-control-and-give-it-to-anoth/293031#293031 – Hans Passant Nov 17 '11 at 22:15

1 Answers1

1

I offer up the following as an alternative path rather than a direct answer... So here goes...

Can you approach this a little differently perhaps by ensuring that your control with the event handler already attached, in this case the parameter someControl, instead implements an interface that defines the attached handler. For example:

public interface ChangUICuesHandler
{
    void OnChangeUICues(object sender, EventArgs e);
}

Then you could use it like so:

Control control = GetControlByName(Console.ReadLine());
if (!(control is ChangeUICuesHandler))
{
    throw new Exception("Input Control does not implement the interface");
}
var handlerControl = control as ChangeUICuesHandler;
button.ChangeUICues += handler.OnChangeUICues;
Reddog
  • 15,219
  • 3
  • 51
  • 63