To create an event procedure that is executed when the MouseDown or MouseUp event occurs, set the OnMouseDown or OnMouseUp property to [Event Procedure], and click the Build button.
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As åSingle, Y As Single)Private Sub controlname_MouseDown(Button As Integer, Shift As Integer, X åAs Single, Y As Single)Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, åY As Single)Private Sub controlname_MouseUp(Button As Integer, Shift As Integer, X As åSingle, Y As Single)
The MouseDown and MouseUp events use the following arguments.
Argument |
Description |
controlname |
A string that is the name of the control affected by the MouseDown and MouseUp event procedures. |
Argument |
Description |
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 Bit mask for the left mouse button. acRightButton Bit mask for the right mouse button. acMiddleButton Bit mask for the middle mouse button. |
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 Bit mask for the SHIFT key. acCtrlMask Bit mask for the CTRL key. acAltMask Bit mask for the ALT key. |
X, Y |
The x and y coordinates for the current location of the mouse pointer. X and Y are always expressed in twips. |
Note If a mouse button is pressed while the pointer is over a form or control, that object receives all mouse events up to and including the last MouseUp event. This means that the X and Y arguments may not always be relative to the object that receives them.
You test for a condition by first assigning each result to a temporary Integer variable and then comparing the Shift or Button argument to a intrinsic constant. Use the And operator with the Button argument to test whether the condition is greater than 0, indicating that the left, middle, or right mouse button was pressed, as in the following example.
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. . .
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.
MouseDown, MouseUp Events — Macros.
The following example shows how you can find out which mouse button caused a MouseDown event.
To try this example, add the following code to the Declarations section of a module for 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." If Button = acRightButton Then _ MsgBox "You pressed the right button." If Button = acMiddleButton Then _ MsgBox "You pressed the middle button."Sub