Description
To create an event procedure that runs when the MouseDown or MouseUp event occurs, set the OnMouseDown or OnMouseUp property to [Event Procedure], and click the Build button.
Syntax Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single,Argument | Description | |
controlname | The name of the control whose MouseDown or MouseUp event procedure you want to run. | |
Button | The button that was pressed (MouseDown) or released (MouseUp) to trigger the event. If you need to test for the Button argument, you can use one of the following intrinsic constants as bit masks: | |
Constant | Description | |
acLeftButton | The bit mask for the left mouse button. | |
acRightButton | The bit mask for the right mouse button. | |
acMiddleButton | The bit mask for the middle mouse button. |
Argument | Description | |
Shift | The state of the SHIFT, CTRL, and ALT keys when the button specified by the Button argument was pressed or released. If you need to test for the Shift argument, you can use one of the following intrinsic constants as bit masks: | |
Constant | Description | |
acShiftMask | The bit mask for the SHIFT key. | |
acCtrlMask | The bit mask for the CTRL key. | |
acAltMask | The bit mask for the ALT key. | |
X, Y | The x and y coordinates for the current location of the mouse pointer. The X and Y arguments are always expressed in twips. |
LeftDown = (Button And acLeftButton) > 0
In an event procedure, you can test for any combination of conditions, as in the following example:
If ShiftDown And CtrlDown Then
. ' Do this if SHIFT and CTRL keys are pressed.
.
.
End If
Note The Button argument for the MouseDown and MouseUp events differs from the Button argument used for the MouseMove event. For the MouseDown and MouseUp events, the Button argument indicates exactly one button per event; that is, if you press two mouse buttons, two MouseDown (and MouseUp) events will occur, each with a different Button argument setting. For the MouseMove event, the Button argument indicates the current state of all buttons.
You can use the arguments for the KeyDown, KeyPress, and KeyUp events, in conjunction with the arguments for the MouseDown, MouseUp, and MouseMove events, to make your application work smoothly for both keyboard and mouse users.
You can't cancel the MouseDown or MouseUp events.See Also MouseDown, MouseUp events — macros.
Example The following example shows how you can find out which mouse button caused a MouseDown event. To try the example, add the following event procedure to a form:Private Sub Form_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y As Single)
If Button = acLeftButton Then
MsgBox "You pressed the left button."
End If
If Button = acRightButton Then
MsgBox "You pressed the right button."
End If
If Button = acMiddleButton Then
MsgBox "You pressed the middle button."
End If
End Sub