Visual Basic Concepts

Responding to Mouse Events

See Also

You can use the MouseDown, MouseUp, and MouseMove events to enable your applications to respond to both the location and the state of the mouse. (This list excludes drag events, which are introduced in "Dragging and Dropping" later in this chapter.) These mouse events are recognized by most controls.

Event Description
MouseDown Occurs when the user presses any mouse button.
MouseUp Occurs when the user releases any mouse button.
MouseMove Occurs each time the mouse pointer is moved to a new point on the screen.

A form can recognize a mouse event when the pointer is over a part of the form where there are no controls. A control can recognize a mouse event when the pointer is over the control.

When the user holds down a mouse button, the object continues to recognize all mouse events until the user releases the button. This is true even when the pointer is moved off the object.

The three mouse events use the following arguments.

Argument Description
Button A bit-field argument in which the three least-significant bits give the status of the mouse buttons.
Shift A bit-field argument in which the three least-significant bits give the status of the SHIFT, CTRL, and ALT keys.
x, y Location of the mouse pointer, using the coordinate system of the object that receives the mouse event.

A bit-field argument returns information in individual bits, each indicating whether a certain condition is on or off. Using binary notation, the three leftmost bits are referred to as most-significant and the three rightmost bits as least-significant. Techniques for programming with these arguments are described in "Detecting Mouse Buttons" and "Detecting SHIFT, CTRL, and ALT States" later in this chapter.