Using the Mouse and Keyboard to Modify Drop Effects and User Feedback

See Also

You can enhance the OLEDragDrop and OLEDragOver events by using the button and shift arguments to respond to the state of the mouse buttons and the SHIFT, CTRL, and ALT keys. For instance, when dragging data into a control, you can allow the user to perform a copy operation by pressing the CTRL key, or a move operation by pressing the SHIFT key.

In the following example, the shift argument of the OLEDragDrop event is used to determine if the SHIFT key is pressed when the data is dropped. If it is, a move is performed. If it is not, a copy is performed.

Private Sub txtTarget_OLEDragDrop(Data As _ 
      VB.DataObject, Effect As Long, Button As _
      Integer, Shift As Integer, X As Single, _
      Y As Single)
   If Shift And vbCtrlMask Then
      txtTarget.Text = Data.GetData(vbCFText)
      Effect = vbDropEffectCopy
   Else
      txtTarget.Text = Data.GetData(vbCFText)
      Effect = vbDropEffectMove
   End If
End Sub

The button argument can be used to isolate and respond to the various mouse button states. For instance, you may want to allow the user to move the data by pressing both the right and left mouse buttons simultaneously.

To indicate to the user what action will be taken when the source object is dragged over the target when a mouse button or the SHIFT, CTRL, and ALT keys are pressed, you can set the shift and button arguments of the OLEDragOver event. For example, to inform the user what action will be taken when the SHIFT button is pressed during a drag operation, you can add the following code to the OLEDragOver event:

Private Sub txtTarget_OLEDragOver(Data As _
      VB.DataObject, Effect As Long, Button As _
      Integer, Shift As Integer, X As Single, _
      Y As Single, State As Integer)
   If Shift And vbCtrlMask Then
      Effect = vbDropEffectCopy
   Else
      Effect = vbDropEffectMove
   End If
End Sub

For More Information   See "Detecting Mouse Buttons" and "Detecting SHIFT, CTRL, and ALT States" for more information on responding to mouse and keyboard states.