Starting the OLE Drag Operation

See Also

If you want to be able to specify which data formats or drop effects (copy, move, or no drop) are supported, or if the control you want to drag from doesn't support automatic dragging, you need to make your OLE drag operation manual.

The first phase of a manual drag-and-drop operation is calling the OLEDrag method, setting the allowed drop effects, specifying the supported data formats, and, optionally, placing data into the DataObject object.

You use the OLEDrag method to manually start the drag operation and the OLEStartDrag event to specify the allowed drop-action effects and the supported data formats.

The OLEDrag Method

Generally, the OLEDrag method is called from an object’s MouseMove event when data has been selected, the left mouse button is pressed and held, and the mouse is moved.

The OLEDrag method does not provide any arguments. Its primary purpose is to initiate a manual drag and then allow the OLEStartDrag event to set the conditions of the drag operation (for example, specifying what will happen when the data is dragged into another control).

If the source control supports the OLEDragMode property, to have manual control over the drag operation you must set the property to Manual and then use the OLEDrag method on the control. If the control supports manual but not automatic OLE drag, it will not have the OLEDragMode property, but it will support the OLEDrag method and the OLE drag-and-drop events.

Note   The OLEDrag method will also work if the source control’s OLEDragMode property is set to Automatic.

Specifying Drop Effects and Data Formats

In a manual OLE drag operation, when the user begins dragging the source and the OLEDrag method is called, the control's OLEStartDrag event fires. Use this event to specify what drop effects and data formats the source supports.

The OLEStartDrag event uses two arguments to specify supported data formats and whether the data can be copied or moved when the data is dropped (drop effects).

Note   If no drop effects or data formats are specified in the OLEStartDrag event, the manual drag will not be started.

The AllowedEffects Argument

The allowedeffects argument specifies which drop effects the drag source supports. For example:

Private Sub txtSource_OLEStartDrag(Data As _ 
      VB.DataObject, AllowedEffects As Long)
   AllowedEffects = vbDropEffectMove Or _ 
      vbDropEffectCopy
End Sub 

The target can then query the drag source for this information and respond accordingly.

The allowedeffects argument uses the following values to specify drop effects:

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 Format Argument

You specify which data formats the object supports by setting the format argument of the OLEStartDrag event. To do this, you use the SetData method. For example, in a scenario using a rich text box control as a source and a text box control as a target, you might specify the following supported formats:

Private Sub rtbSource_OLEStartDrag(Data As _
      VB.DataObject, AllowedEffects As Long)
   AllowedEffects = vbDropEffectMove Or _
      vbDropEffectCopy

   Data.SetData , vbCFText
   Data.SetData , vbCFRTF
End Sub

The target can query the source to determine which data formats are supported and then respond accordingly — e.g., if the format of the dropped data is not supported by the target, reject the dropped data. In this case, the only data formats that are supported by the source are the text and rich-text formats.

For More Information   See "The OLE Drag and Drop DataObject Object" for more information on format values for the SetData method.

Placing Data into the DataObject object

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. You can, however, place the data into the DataObject object when you begin a drag operation by using the SetData method in the OLEStartDrag event. For example:

Private Sub txtSource_OLEStartDrag(Data As _ 
      VB.DataObject, AllowedEffects As Long)
   Data.Clear
   Data.SetData txtSource.SelText, vbCFText
End Sub 

This example clears the default data formats from the DataObject object using the Clear method, specifies the data format (text) of the selected data, and then places the data into the DataObject object with the SetData method.