Visual Basic Concepts

Dropping the OLE Drag Source onto the OLE Drop Target

See Also

If your target supports manual OLE drag-and-drop operations, you can control what happens when the cursor is moved within the target and can specify what kind of data the target will accept. When the user drops the source object onto the target control, the OLEDragDrop event is used to query the DataObject object for a compatible data format, and then retrieve the data.

The OLEDragDrop event also informs the source of the drop action, allowing it to delete the original data if a move has been specified, for example.

Retrieving the Data

The OLEDragDrop event is triggered when the user drops the source onto the target. If data was placed into the DataObject object when the drag operation was initiated, it can be retrieved when the OLEDragDrop event is triggered, by using the GetData method. If, however, only the supported source formats were declared when the drag operation was initiated, then the GetData method will automatically trigger the OLESetData event on the source to place the data into, and then retrieve the data from, the DataObject object.

The following example retrieves data that was placed into the DataObject object when the drag operation was initiated. The drag operation may have been initiated manually (using the OLEDrag method on the source) or automatically (by setting the OLEDragMode property of the source to Automatic). The dragged data is retrieved using the DataObject object’s GetData method. The GetData method provides you with constants that represent the data types that the DataObject object supports. In this case, we are retrieving the data as text.

Private Sub txtTarget_OLEDragDrop(Data As _ 
      VB.DataObject, Effect As Long, Button As _
      Integer, Shift As Integer, X As Single, _
      Y As Single)
   txtTarget.Text = Data.GetData(vbCFText)
End Sub

For More Information   For a complete list of GetData format constants, see "The OLE Drag and Drop DataObject Object" earlier in this chapter.

Querying the DataObject Object

You may need to query the DataObject object for the types of data that are being dropped onto the target. You use the GetFormat method in an If…Then statement to specify which types of data the target control can accept. If the data within the DataObject object is compatible, the drop action will be completed.

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

Placing Data into the DataObject Object

When the target uses the GetData method to retrieve data from the source, the OLESetData event is only triggered if the data was not placed into the source when the drag operation was initiated.

In many cases, especially if the source supports more than one format, or if it is time-consuming to create the data, you may want to place data into the DataObject object only when it is requested by the target. The OLESetData event allows the source to respond to only one request for a given format of data.

For example, if the supported data formats were specified using the OLEStartDrag event when the drag operation was initiated, but data was not placed into the DataObject object, the OLESetData event is used to place a specific format of data into the DataObject object.

Private Sub txtSource_OLESetData(Data As _
      VB.DataObject, DataFormat As Integer)
   If DataFormat = vbCFText Then
      Data.SetData txtSource.SelText, vbCfText
   End If
End Sub

Informing the Source When Data is Dropped

The effect argument of the OLEDragDrop event specifies how the data was incorporated into the target when the data was dropped. When this argument is set, the OLECompleteDrag event is triggered on the source with its effect argument set to this value. The source can then take the appropriate action: If a move is specified, the source deletes the data, for example.

The effect argument of the OLEDragDrop event uses the same constants as the effect argument of the OLEDragOver event to indicate the drop action. The following table lists these constants:

Constant Value Description
vbDropEffectNone 0 Drop target cannot accept the data.
vbDropEffectCopy 1 Drop results in a copy. The original data is untouched by the drag source.
vbDropEffectMove 2 Drag source removes the data.

The following example sets the effect argument to indicate the drop action.

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

On the source side, the OLECompleteDrag event is triggered when the source is dropped onto the target, or when the OLE drag-and-drop operation is canceled. OLECompleteDrag is the last event in the drag-and-drop operation.

The OLECompleteDrag event contains only one argument (effect), which is used to inform the source of the action that was taken when the data is dropped onto the target.

The effect argument returns the same values that are used by the effect argument of the other OLE drag-and-drop events: vbDropEffectNone, vbDropEffectCopy, and vbDropEffectMove.

By setting this argument after a move has been specified by the target and the source has been dropped into the target, for example, the source will delete the original data in the control. You should also use the OLECompleteDrag event to reset the mouse pointer if you specified a custom mouse pointer in the OLEGiveFeedback event. For example:

Private Sub txtSource_OLECompleteDrag(Effect As Long)
   If Effect = vbDropEffectMove Then
      txtSource.SelText = ""
   End If
   Screen.MousePointer = vbDefault
End Sub