You cannot "fire" (invoke) any event anywhere, except the type where the event is declared, not even in a derived class. This is important limitation (compared to "regular" delegate instances), which makes events so safe and valuable. In case of the
Click
event, it will be fired when a user actually clicks with a physical mouse. If could be simulated, but on a low level, via raw Windows API
SendInput
,
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[
^],
http://www.pinvoke.net/default.aspx/user32.sendinput[
^].
I don't think you want it.
The thing is: I'm sure you
don't actually need to "fire" and event. You just need to have
the same effect as you would have when the event is invoked. This would be a different story, much more reasonable thing. Do the following:
Add only one handler to the invocation list of the required
Click
event. In this handler, call some separate function; this should be the only statement in your event handler. And write a call to this function elsewhere, where you wanted to simulate the click. As simple as that.
—SA