OLE drag-and-drop uses the same source and target model as the simple event-driven drag-and-drop techniques discussed in “Dragging and Dropping.” In this case, however, you’re not dragging one control to another control to invoke some code; you’re moving data from one control or application to another control or application. For example, the user selects and drags a range of cells in Excel (source) then drops the range of cells into the DataGrid control (target) in your application.
In Visual Basic, the vehicle, or repository, of this data is the DataObject object — it is the means by which data is moved from the source to the target. It does this by providing the methods needed to store, retrieve, and analyze the data. The following table lists the property and methods used by the DataObject object:
Category | Item | Description |
Property | Files | Holds the names of files dragged to or from the Windows Explorer. |
Methods | Clear | Clears the content of the DataObject object. |
GetData | Retrieves data from the DataObject object. | |
GetFormat | Determines if a specified data format is available in the DataObject object. | |
SetData | Places data into the DataObject object, or indicates that a specified format is available upon request. |
Used with the OLE drag-and-drop events, these methods allow you to manage data in the DataObject object on both the source and target sides (if both are within your Visual Basic application). For instance, you can place data into the DataObject object on the source side using the SetData method, and then use the GetData method to accept the data on the target side.
The Clear method is used to clear the content of the DataObject object on the source side when the OLEStartDrag event is triggered. When data from a control is dragged in an automatic drag operation, its data formats are placed into the DataObject object before the OLEStartDrag event is triggered. If you don’t want to use the default formats, you use the Clear method. If you want to add to the default data formats, you do not use the Clear method.
The Files property allows you to store the names of a range of files that can be then dragged into a drop target. See “Dragging Files from the Windows Explorer” for more information on this property.
You can also specify the format of the data being transferred. The SetData and GetData methods use the following arguments to place or retrieve data in the DataObject object:
Argument | Description |
Data | Allows you to specify the type of data that is placed into the DataObject object (optional argument if the format argument has been set; otherwise, it's required). |
Format | Allows you to set several different formats that the source can support, without having to load the data for each (optional argument if the data argument has been set or if Visual Basic understands the format; otherwise, it's required). |
Note When data is dropped onto the target and no format has been specified, Visual Basic is able to detect if it is a bitmap, metafile, enhanced metafile, or text. All other formats must be specified explicitly or an error will be generated.
The format argument uses the following constants or values to specify the format of the data:
Constant | Value | Meaning |
vbCFText | 1 | Text |
vbCFBitmap | 2 | Bitmap (.bmp) |
vbCFMetafile | 3 | Metafile (.wmf) |
vbCFEMetafile | 14 | Enhanced metafile (.emf) |
vbCFDIB | 8 | Device-independent bitmap (.dib or .bmp) |
vbCFPalette | 9 | Color palette |
vbCFFiles | 15 | List of files |
vbCFRTF | -16639 | Rich text format (.rtf) |
The SetData, GetData, and GetFormat methods use the data and format arguments to return either the type of data in the DataObject object or to retrieve the data itself if the format is compatible with the target. For example:
Private Sub txtSource_OLEStartDrag(Data As _
VB.DataObject, AllowedEffects As Long)
Data.SetData txtSource.SelText, vbCFText
End Sub
In this example, data is the text selected in a textbox and format has been specified as text (vbCFText).
Note You should use the vbCFDIB data format instead of vbCFBitmap and vbCFPalette, in most cases. The vbCFDIB format contains both the bitmap and palette and is therefore the preferred method of transferring a bitmap image. You can, however, also specify the vbCFBitmap and vbCFPalette for completeness. If you chose not to use the vbCFDIB format, you must specify both the vbCFBitmap and vbCFPalette formats so that the bitmap and the palette are correctly placed into the DataObject object.
For More Information See "Creating a Custom Data Format" for information on defining your own data format.