Raising event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

how can raise an event that is handled by an event handler? I simply want to
execute the code that is attached to a 'click'- event of a button from
anywhere in my code. How can I do this?

Thanks
Peter
 
You can raise an event one of several ways. If you are in a class that has
come onXXX methods, then you can simply call that or you can call the event
directly by simply making a call to the event directly (such as Click(this,
new EventArgs). Both these will invoke all handlers.

Another way is that you can call the method directly. A handler is nothing
more then a method and you can call it has you would any other method.

As an alternative to the above (an in my opinion a much better design),
would be to move the logic out of the event handler into some other method.
That way you can call the method and the handler is only responsible for
calling the method. Normally I try to keep as much logic out of the event
handlers as possible. It makes the design more flexible because then you
can easily call the logic without worrying about event specific constructs
such as passing in the EventArgs object.
 
Hi Peter

I would put the code from your click event in a separate function and call
that.

HTH

Charles
 
how can raise an event that is handled by an event handler? I simply want to
execute the code that is attached to a 'click'- event of a button from
anywhere in my code. How can I do this?

Buttons have a PerformClick method.



Mattias
 
Back
Top