The Mouse Down Event in C# .NET

The Mouse Down Event in C# .NET

You can see what event are available for a particular control in the Properties area.
In Design View, click on your Form1 to select it, instead of the button. To see what events are available for the Form itself, click the lightning bolt at the top of the Properties area, as in the image below:
The C# Properties area with the Events icon circled
When you click the lightning bolt, you'll see a list of events appear:
A list of Form Events in C# .NET
You've already met the Load event, so we won't cover it here. But notice how many events there are.
Locate the MouseDown event on the list. Now double click the word "MouseDown". You should see a code stub appear:
C# code stub for the MouseDown Event
In between the round brackets, there is still a sender object. But notice the new argument:
MouseEventArgs e
The letter "e" is the default variable name. The type of variable belongs to the MouseEventArgs. You can see what this does by typing the letter "e", then a full stop (period). You should see the IntelliSense list appear:
The IntelliSense list for the e Event
The list is displaying the properties and methods available to the e variable. One of these is the Button property.
We can use an if statement to check which mouse button was clicked. Add this to your code:
C# code to detect which button was clicked
Run your programme and click either of your mouse buttons on the form. You should see a message box display.

Comments