Browse DevX
Sign up for e-mail newsletters from DevX


Using OLE Drag-and-Drop in Visual Basic 6 : Page 4

There are two types of Drag-and-Drop functionality in Visual Basic: the traditional kind and OLE. This second type of drag-and-drop allows users to drag data between applications as well as within an application. However, it only works with applications that support it. Luckily, this includes most Microsoft applications and many programs from other vendors as well. In this article, learn how to use this basic but essential functionality.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

A Practical Example
Here's an example. Suppose the variable X contains some text data, and myDO refers to a DataObject object. The following code makes the data in X available in myDO as text, as rich text, and as a bitmap:

myDO.SetData X, vbCFText myDO.SetData , vbCFRTF myDO.SetData , vbCFBitmap

The GetFormat() method returns a true/false value indicating whether data is available in the specified format. The method takes a single argument, one of the format constants from Table 3, and it returns True only if the DataFormat object contains data in the specified format. The drag-and-drop related events work together, and the relationship between them is somewhat confusing. Some actions cause events to fire in both the source and target objects. Table 4 explains how this works.

Table 4. User actions and corresponding OLE drag-and-drop events. 


Event procedure(s) fired

What your code does

Drag operation starts

OLEStartDrag (source)

Set the effects (move, copy) that are permitted. Put the data being dragged in the DataObject.

Cursor moves over a potential target.

OLEDragOver (target), then OLEGiveFeedback (source).

In OLEDragOver: Examine the format of data being dragged and set the mouse cursor to indicate whether a drop is permitted. In OLEGiveFeedback: change the appearance of the source control if desired (optional).

Object is dropped.

OLEDragDrop (target) and then OLECompleteDrag (source).

In OLEDragDrop: check the format of the data and retrieve it if it is acceptible. In OLECompleteDrag: erase the data from the souce control (only if the data is being moved instead of copied).

The OLEStartDrag event procedure, which fires when a manual OLE drag-and-drop operation begins, has an AllowedEffects argument which you use to specify the drag-and-drop effects which will be permitted. The constants for this argument are listed in Table 5, and can be combined with the Or operator. Which of the allowed operations actually occurs (at the end of the operation when the drop is done) is determined elsewhere in code.

Table 5. Constants for the effects argument.






Drop target cannot accept the data.



Drop results in data being copied from source to target.



Drop results in data being moved from source to target.

The OLEDragOver, OLEDragDrop, OLECompleteDrag, and OLEGiveFeedback event procedures have an effects argument that is used to specify and to determine the drag-and-drop operations permitted. You use the constants listed in Table 5 for this argument. Code in these event procedures can read the effects argument to see what effects have been permitted by the source, and can also change the value as needed. You'll see how this works in the sample application to be developed later.

Thanks for your registration, follow us on our social networks to keep up-to-date