How to programmatically raise an event in C#

Some say you can’t.

Other repeatedly and annoyingly say “just call the callback manually, and pass (null, null) for the args”.

Other still find the answer is YOU CAN!!

So, for a Button:

button1.PerformClick() ;

The form itself has a whole series of functions…

this.OnClick( ... );
this.OnClientSizeChanged( ... ) ;

// And
this.RaiseMouseEvent( ... ) ; // (pretty much) undocumented. nobody seems to know how to use this one!

There’s also this REALLY COMPLICATED way using Interop and the SendMessage() WinAPI function. You don’t have to go there though.

Post a Comment