Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise MouseClick event?

Tags:

c#

events

Hello I have read that events can be raised the same way as methods. Well it works for my custom events (I create a delegate, the event and I am able to raise the event by calling it). However I am not able to manually raise events like MouseClick and other, it keeps saying that it must appear on the left side of the += operator. What is the problem?

like image 819
Calamro Avatar asked Nov 16 '25 05:11

Calamro


2 Answers

While I am certain you'll get other answers more informative than this one, basically you can't "raise" an event outside the class that contains it. MSDN has this to say about events

Events are a special kind of multicast delegate that can only be invoked from within the class or struct where they are declared (the publisher class). If other classes or structs subscribe to the event, their event handler methods will be called when the publisher class raises the event.

If you wanted to literally raise the event for, say, a Windows Forms Control MouseClick, you'd have to create a subclass of that control and either invoke base.OnMouseClick() or override it.

like image 111
Nick Spreitzer Avatar answered Nov 18 '25 20:11

Nick Spreitzer


If this is a button, you can programmatically click it using the PerformClick method.

Sadly, this only works on buttons and not other types of Controls... except MenuItem.

like image 27
Powerlord Avatar answered Nov 18 '25 19:11

Powerlord