MouseMove Event

Applies To

CheckBox control, ComboBox control, CommandButton control, Frame control, Image control, Label control, ListBox control, MultiPage control, OptionButton control, TabStrip control, TextBox control, ToggleButton control, UserForm object.

Description

Occurs when the user moves the mouse.

Syntax

For MultiPage, TabStrip:

Private Sub object_MouseMove( index As Long, ByVal Button As fmButton,
úByVal Shift As fmShiftState, ByVal X As Single, ByVal Y As Single)

For other controls:

Private Sub object_MouseMove( ByVal Button As fmButton, ByVal Shift As
úfmShiftState, ByVal X As Single, ByVal Y As Single)

The MouseMove event syntax has these parts:

Part

Description

object

Required. A valid object name.

index

Required. The index of the page or tab in a MultiPage or TabStrip associated with this event.

Button

Required. An integer value that identifies the state of the mouse buttons.

Shift

Required. Specifies the state of SHIFT, CTRL, and ALT.

X, Y

Required. The horizontal or vertical position, measured in points, from the left or top edge of the control.


Settings

The index argument specifies which page or tab was clicked over. A 1 designates that the user did not click on any of the pages or tabs.

The settings for Button are:

Value

Description

0

No button is pressed.

1

The left button is pressed.

2

The right button is pressed.

3

The right and left buttons are pressed.

4

The middle button is pressed.

5

The middle and left buttons are pressed.

6

The middle and right buttons are pressed.

7

All three buttons are pressed.


The settings for Shift are:

Value

Description

1

SHIFT was pressed.

2

CTRL was pressed.

3

SHIFT and CTRL were pressed.

4

ALT was pressed.

5

ALT and SHIFT were pressed.

6

ALT and CTRL were pressed.

7

ALT, SHIFT, and CTRL were pressed.


You can identify individual keyboard modifiers by using the following constants:

Constant

Value

Description

fmShiftMask

1

Mask to detect SHIFT.

fmCtrlMask

2

Mask to detect CTRL.

fmAltMask

4

Mask to detect ALT.


Remarks

The MouseMove event applies to forms, controls on a form, and labels.

MouseMove events are generated continually as the mouse pointer moves across objects. Unless another object has captured the mouse, an object recognizes a MouseMove event whenever the mouse position is within its borders.

Moving a form can also generate a MouseMove event even if the mouse is stationary. MouseMove events are generated when the form moves underneath the pointer. If a macro or event procedure moves a form in response to a MouseMove event, the event can continually generate (cascade) MouseMove events.

If two controls are very close together, and you move the mouse pointer quickly over the space between them, the MouseMove event might not occur for that space. In such cases, you might need to respond to the MouseMove event in both controls.

You can use the value returned in the Button argument to identify the state of the mouse buttons.

Use the Shift argument to identify the state of SHIFT, CTRL, and ALT when the MouseMove event occurred. For example, if both CTRL and ALT are pressed, the value of Shift is 6.

Note You can use MouseDown and MouseUp event procedures to respond to events caused by pressing and releasing mouse buttons.

See Also

MouseDown, MouseUp events.

Example

The following example demonstrates a drag-and-drop operation from one ListBox to another using a DataObject to contain the dragged text. This code sample uses the SetText and StartDrag methods in the MouseMove event to implement the drag-and-drop operation.

To use this example, copy this sample code to the Declarations portion of a form. Make sure that the form contains two ListBox controls named ListBox1 and ListBox2. You also need to add choices to the second ListBox.

Private Sub ListBox2_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As _
Single, ByVal DragState As Long, ByVal Effect As MSForms.Return _
Effect, ByVal Shift As Integer) Cancel = True Effect = 1 End Sub Private Sub ListBox2_BeforeDropOrPaste(ByVal Cancel As MSForms.Return _
Boolean, ByVal Action As Long, ByVal Data As MSForms.DataObject, _
ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms. _
ReturnEffect, ByVal Shift As Integer) Cancel = True Effect = 1 ListBox2.AddItem Data.GetText End Sub Private Sub ListBox1_MouseMove(ByVal Button As Integer, ByVal Shift As _
Integer, ByVal X As Single, ByVal Y As Single) Dim MyDataObject As DataObject If Button = 1 Then Set MyDataObject = New DataObject Dim Effect As Integer MyDataObject.SetText ListBox1.Value Effect = MyDataObject.StartDrag End If End Sub Private Sub UserForm_Initialize() For i = 1 To 10 ListBox1.AddItem "Choice " & (ListBox1.ListCount + 1) Next i End Sub