How OLE Drag and Drop Works

See Also

When an OLE drag-and-drop operation is performed, certain events are generated on the source and target sides. The events associated with the source object are always generated, whether the drag-and-drop operation is automatic or manual. The target-side events, however, are only generated in a manual drop operation. The following illustration shows which events occur and can be responded to on the drag source, and which occur and can be responded to on the drop target.

Figure 11.6   Source-side and target-side events

Which events you’ll need to respond to depends upon how you’ve chosen to implement the drag-and-drop functionality. For example, you may have created an application with a text box that you want to allow to automatically accept dragged data from another application. In this case, you simply set the control’s OLEDropMode property to Automatic. If you want to allow data to be automatically dragged from the text box control as well, you set its OLEDragMode property to Automatic.

If, however, you want to change the default mouse cursors or enhance the functionality for button states and shift keys, you need to manually respond to the source- and target-side events. Likewise, if you want to analyze the data before it is dropped into a control (to verify that the data is compatible, for instance), or delay when the data is loaded into the DataObject object (so that multiple formats don't need to be loaded at the beginning), you'll need to use manual OLE drag-and-drop operations.

Because you can drag and drop data into numerous Visual Basic controls and Windows applications — with varying limitations and requirements — implementing OLE drag and drop can range from straightforward to fairly complex. The simplest implementation, of course, would be dragging and dropping between two automatic objects, whether the object is a Word document, an Excel spreadsheet, or a control in your application that has been set to Automatic. Specifying multiple data formats that would be acceptable to your drop target would be more complicated.

Starting the Drag

What happens in a basic manual OLE drag-and-drop operation within your Visual Basic application? When the user drags data from an OLE drag source (a text box control, for example) by selecting and then holding down the left mouse button, the OLEStartDrag event is triggered and you can then either store the data or simply specify the formats that the source supports. You also need to specify whether copying or moving the data, or both, is allowed by the source.

For More Information   See "Starting the OLE Drag Operation" for more information on the OLEDrag method, the OLEstartDrag event, using the SetData method to specify the supported data formats, and placing data into the DataObject.

Dragging Over the Target

As the user drags over the target, the target’s OLEDragOver event is triggered, indicating that the source is within its boundaries. You then specify what the target would do if the data were dropped there — either copy, move, or refuse the data. By convention, the default is usually move, but it may be copy.

When the target specifies which drop effect will be performed if the source is dropped there, the OLEGiveFeedback event is triggered. The OLEGiveFeedback event is used to provide visual feedback to the user on what action will be taken when the selection is dropped — i.e., the mouse pointer will be changed to indicate a copy, move, or "no drop" action.

As the source is moved around within the boundaries of the target — or if the user presses the SHIFT, CTRL, or ALT keys while holding down the mouse button — the drop effect may be changed. For example, instead of allowing a copy or a move, the data may be refused.

If the user passes beyond the target or presses the ESC key, for example, then the drag operation may be canceled or modified (the mouse pointer may be changed to indicate that the object it is currently passing over will not accept the data).

For More Information   See "Dragging the OLE Drag Source over the OLE Drop Target" for more information on the OLEDragOver and OLEGiveFeedback events.

Completing the Drag

When the user drops the source onto the target, the target’s OLEDragDrop event is triggered. The target queries the source for the format of the data it contains (or supports, if the data wasn’t placed into the source when the drag was started) and then either retrieves or rejects the data.

If the data was stored when the drag started, the target retrieves the data by using the GetData method. If the data wasn’t stored when the drag started, the data is retrieved by triggering the source’s OLESetData event and then using the SetData method.

When the data is accepted or rejected, the OLECompleteDrag event is triggered and the source can then take the appropriate action: if the data is accepted and a move is specified, the source deletes the data, for example.

For More Information   See “Dropping the OLE Drag Source onto the OLE Drop Target” for more information on the OLEDragDrop event, the OLECompleteDrag event, and using the GetFormat and GetData methods to retrieve data from the DataObject object.