Raising event

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
 
P

Peter Rilling

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.
 
C

Charles Law

Hi Peter

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

HTH

Charles
 
M

Mattias Sjögren

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top