Discussion:
Raising event
(too old to reply)
Peter Schmitz
2005-02-23 18:57:05 UTC
Permalink
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
Charles Law
2005-02-23 19:08:10 UTC
Permalink
Hi Peter

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

HTH

Charles
Post by Peter Schmitz
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
Peter Rilling
2005-02-23 19:07:41 UTC
Permalink
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.
Post by Peter Schmitz
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
Mattias Sjögren
2005-02-23 23:21:05 UTC
Permalink
Post by Peter Schmitz
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
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Loading...