Tutorials > Tutorial 14 - OLE Drag-and-Drop |
In this tutorial, you will learn how to use the OLE drag-and-drop features of True DBGrid. Unlike the Visual Basic drag-and-drop demonstrated in Tutorial 13, which invokes code as one control is dragged or dropped over another, OLE drag-and-drop moves data from a source component to a target component. These components can be controls on a Visual Basic form or standalone applications. For example, you can select a range of text in Word 97 or Access 97, then drop it into a True DBGrid control in your own application.
Start a new project.
Place a Data control (Data1), a True DBGrid control (TDBGrid1), and a TextBox control (Text1) on the form (Form1) as shown in the following figure.
Set the DatabaseName property of Data1 to TDBGDemo.MDB, and the RecordSource property to Composer.
Set the DataSource property of TDBGrid1 to Data1.
Set the AllowAddNew and AllowDelete properties of TDBGrid1 to True (note that the default value of AllowUpdate is True).
Set the MultiLine property of Text1 to True. Type several lines of text into the Text property of Text1.
Set the OLEDragMode property of both TDBGrid1 and Text1 to 1 - Automatic, and the OLEDropMode property of both controls to 2 - Automatic.
Run the program and observe the following:
Select an arbitrary range of text in Text1. Move the mouse over the selected text, then hold down the left mouse button and drag it to the current cell of TDBGrid1. When you start dragging, the mouse pointer turns into a drag icon, indicating a move operation.
As you drag over TDBGrid1, the mouse pointer turns into a "No drop allowed" symbol, since True DBGrid only accepts data dropped into the current row or cell.
As soon as you move the mouse over the current row, the mouse pointer turns into a drag icon, indicating that TDBGrid1 is ready to accept data.
When you release the mouse button, the current cell of TDBGrid1 receives the text selection from Text1, and the selected text is removed from Text1. If you want to copy the selection instead of moving it, hold down the Ctrl key while dragging. The mouse pointer will indicate that a copy operation is in progress by changing its icon to the following symbol.
This feature enables you to transfer data between controls and applications that support OLE drag-and-drop. If you have Word 97 or Access 97, feel free to try it with this project. To gain additional control over the process, or to implement custom behavior, consider using the following events: OLECompleteDrag, OLEDragDrop, OLEDragOver, OLEGiveFeedback, OLESetData, and OLEStartDrag.
This concludes Tutorial 14.