Handling Drop Events


The following objects do not provide automatic drop operations: Form, Label, Command Button, Frame, Check Box, Option Button, Combo Box, List Box, Drive List, Dir List, File List, and Bound Data control. To allow users to drop OLE data on any of these objects, or to modify the drop behavior of other objects, set the OLEDropMode property to Manual and use the events described in Table 19.2 to respond to drop operations.

Table 19.2 - Events to Use When Performing Manual Drop Operations

Event Occurs When Use to
OLEDragOver A source object is dragged over a control Test for valid data
OLEDragDrop A source object is dropped onto a controlPerform drop action

Use the OLEDragOver event to provide visual feedback during a drop operation. Use the Data parameter to check if the type of data is valid for the target object, and use the Effect parameter to change the mouse pointer to let the user know whether or not the data is valid for the target. For instance, the code in Listing 19.1 lets the user drag text data onto a form; if any other data is dragged onto the form, the vbNoDrop mouse pointer is displayed and the drop is prevented.

Listing 19.1 - Using OLEDragOver to Drop Text on a Form.


' Form properties:
'   OLEDropMode = Manual
Private Sub Form_OLEDragOver(Data As DataObject, _

Effect As Long, Button As Integer, Shift As Integer, _ X As Single, Y As Single, State As Integer) ' If the data is text, allow drop. If Data.GetFormat(vbCFText) Then Effect = vbDropEffectCopy ' Otherwise, prohibit drop (displays vbNoDrop ' mouse pointer). Else Effect = vbDropEffectNone End If End Sub

Use the OLEDragDrop event to perform the action of the drop operation. For instance, the preceding code allows text to be dropped on a form. Listing 19.2 prints the dragged text data on the form after the drop operation.

Listing 19.2 - Using the OLEDragDrop Event to Displays Text Dropped on a Form


Dim mstrBackground As String
Private Sub Form_OLEDragDrop(Data As DataObject, _ 
  Effect As Long, Button As Integer, Shift As Integer, _
  X As Single, Y As Single)
   ' Save the text for later drag operation.
   mstrBackground = Data.GetData(vbCFText)
   ' Print the text on the form background.
   Print mstrBackground
End Sub