An essential aspect of Windows programming is the use of the mouse.
Bear in mind that the mouse is hardware and not software. Windows will detect what happens with the mouse and generate events as appropriate. It sends an object of type MouseEventArgs (or in some cases EventArgs, the base class) to each handler as its second argument.
Mouse Events
There are several kinds of mouse events: (list from an MSDN page)
This event occurs when the mouse button is released, typically before the MouseUp event. The handler for this event receives an argument of type EventArgs. Handle this event when you only need to determine when a click occurs.
This event occurs when the user clicks the control with the mouse. The handler for this event receives an argument of type MouseEventArgs. Handle this event when you need to get information about the mouse when a click occurs.
This event occurs when the control is double-clicked. The handler for this event receives an argument of type EventArgs. Handle this event when you only need to determine when a double-click occurs.
This event occurs when the user double-clicks the control with the mouse. The handler for this event receives an argument of type MouseEventArgs. Handle this event when you need to get information about the mouse when a double-click occurs.
This event occurs when the mouse pointer is over the control and the user presses a mouse button. The handler for this event receives an argument of type MouseEventArgs.
This event occurs when the mouse pointer enters the border or client area of the control, depending on the type of control. The handler for this event receives an argument of type EventArgs.
This event occurs when the mouse pointer stops and rests over the control. The handler for this event receives an argument of type EventArgs.
This event occurs when the mouse pointer leaves the border or client area of the control, depending on the type of the control. The handler for this event receives an argument of type EventArgs.
This event occurs when the mouse pointer moves while it is over a control. The handler for this event receives an argument of type MouseEventArgs.
This event occurs when the mouse pointer is over the control and the user releases a mouse button. The handler for this event receives an argument of type MouseEventArgs.
This event occurs when the user rotates the mouse wheel while the control has focus. The handler for this event receives an argument of type MouseEventArgs. You can use the Delta property of MouseEventArgs to determine how far the mouse has scrolled.
As you may gather, EventArgs provides less information for us than MouseEventArgs.
MouseEventArgs Class
This gives us information about what has just happened with the mouse.
There is a constructor, but it does not seem useful.
Properties: (all "get" only)
How do we use this?
Suppose we want to handle MouseDown events. We will need a using statement for System.Windows.Forms.
In the InitializeComponent method, we need a line:
this.MouseDown += new MouseEventHandler(this.Form_MouseDown);
The event handler might look like this:
private void Form_MouseDown(object sender, MouseEventArgs e) { switch (e.Button) { case MouseButtons.Left: MessageBox.Show("Left Button Click"); break; case MouseButtons.Right: MessageBox.Show("Right Button Click"); break; default: break; } }
In the Designer file, we need a line to attach the handler to the event:
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form_MouseClick);