Visual Basic Concepts

OLE Drag and Drop

See Also

One of the most powerful and useful features you can add to your Visual Basic applications is the ability to drag text or graphics from one control to another, or from a control to another Windows application, and vice versa. OLE drag-and-drop allows you to add this functionality to your applications.

With OLE drag and drop, you’re not dragging one control to another control to invoke some code (as with the drag and drop discussed earlier in this chapter); you’re moving data from one control or application to another control or application. For example, you can select and drag a range of cells in Excel and then drop the range of cells into a DataGrid control in your application.

Nearly all Visual Basic controls support OLE drag-and-drop to some degree. In addition, some standard and ActiveX controls (those provided in the Professional and Enterprise editions of Visual Basic) provide automatic support for OLE drag-and-drop, which means that the control supports automatic settings in both their OLEDragMode and OLEDropMode properties, and that no code needs to be written to either drag from or drop to the control. This is opposed to manual dragging and dropping, in which the behavior of the drag or drop must be programmed by you.

Some of the controls that support both automatic OLEDragMode and OLEDropMode include the PictureBox, Label, and TextBox controls, among others. To enable automatic OLE dragging and dropping for these controls, set both the OLEDragMode and OLEDropMode properties to Automatic.

Some controls support automatic OLE dragging but only manual dropping, and some support automatic OLE dropping but only manual dragging. For instance, the ComboBox control supports both manual and automatic dragging, but doesn’t support automatic dropping. This is because if, for example, you drag an item into a ComboBox, Visual Basic has no way of knowing exactly where to place the new item. However, manual dropping is available so that you can programmatically place items wherever you like in the ComboBox. To enable automatic dragging from these controls, set the OLEDragMode property to Automatic.

Some controls support only the manual OLE drag-and-drop events, meaning that you can program them to act either as the source or target of the OLE drag-and-drop operations.

Note   To determine if other ActiveX controls support OLE drag and drop, load the control into Visual Basic and check for the existence of the OLEDragMode and OLEDropMode properties, or for the OLEDrag method. (A control that does not have automatic support for OLE drag will not have the OLEDragMode property, but it will have an OLEDrag method if it supports OLE drag through code.)

Note   Forms, MDI forms, Document Objects, User Controls, and Property Pages contain the OLEDropMode property and provide support for manual dragging and dropping only.

Using the following OLE drag-and-drop properties, events, and method, you can specify how a given control responds to dragging and dropping.

Category Item Description
Properties OLEDragMode Enables automatic or manual dragging of a control (if the control supports manual but not automatic OLE drag, it will not have this property but it will support the OLEDrag method and the OLE drag-and-drop events).
  OLEDropMode Specifies how the control will respond to a drop.
Events OLEDragDrop Recognizes when a source object is dropped onto a control.
  OLEDragOver Recognizes when a source object is dragged over a control.
OLEGiveFeedback Provides customized drag icon feedback to the user, based on the source object.
OLEStartDrag Specifies which data formats and drop effects (copy, move, or refuse data) the source supports when dragging is initiated.
OLESetData Provides data when the source object is dropped.
OLECompleteDrag Informs the source of the action that was performed when the object was dropped into the target.
Method OLEDrag Starts manual dragging.

Automatic vs. Manual Dragging and Dropping

It is helpful to think of OLE drag-and-drop implementation as either automatic or manual.

Automatic dragging and dropping means that, for example, you can drag text from one text box control to another by simply setting the OLEDragMode and OLEDropMode properties of these controls to Automatic: You don’t need to write any code to respond to any of the OLE drag-and-drop events. When you drag a range of cells from Excel into a Word document, you’ve performed an automatic drag-and-drop operation. Depending upon how a given control or application supports OLE drag and drop and what type of data is being dragged, automatically dragging and dropping data may be the best and simplest method.

Manual dragging and dropping means that you have chosen (or have been forced to) manually handle one or more of the OLE drag-and-drop events. Manual implementation of OLE drag and drop may be the better method when you want to gain greater control over each step in the process, to provide the user with customized visual feedback, to create your own data format. Manual implementation is the only option when a control does not support automatic dragging and dropping.

It is also helpful to define the overall model of the OLE drag-and-drop operation. In a drag and drop operation, the object from which data is dragged is referred to as the source. The object into which the data is dropped is referred to as the target. Visual Basic provides the properties, events, and method to control and respond to actions affecting both the source and the target. It is also helpful to recognize that the source and the target may be in different applications, in the same application, or even in the same control. Depending upon the scenario, you may need to write code for either the source or target, or both.