OLE Drag-and-Drop Operations

You learned how to program drag-and-drop operations. Drag-and drop operations allow the user to drag a control on a Form and drop it on another control (which may reside on the same or another Form). The destination control knows when another control is dropped and can be programmed to react to this action. However; you must supply some code in the destination control’s Drag- Drop event or nothing will happen .

Many applications allow you to drag-and-drop not only controls, but also documents, or parts of documents. For example, you can select a range of cells in an Excel worksheet and drop them on a Word document. If you use the right mouse button for this operation, you can link the source data into the destination document. This operation is called OLE drag-and-drop, because it relies on OLE (you can drag-and drop parts of documents only among applications that support OLE).

To add OLE drag-and-drop capabilities to certain controls on a Form, you must set the following properties of the controls:

OLE Drag Mode Determines whether a control can initiate a drag-and-drop operation, similar to the DragMode. This property can be set to {}-Manual (the control can be dragged under program control) or l-Automatic (the control can be dragged automatically).
. – .
.OLE Drop Mode This property is equivalent to the DropMode property and determines whether a control can act as the destination of an OLE drag and-drop operation. This property can be SE?t to {}-None (the control can’t be used as a destination), l-Manual (the control must be programmed to react to OLE drag-and-drop operations), or 2-Automatic (the control automatically becomes the destination). The simplest method to incorporate OLE drag-and-drop functionality in your applications is to set the OLEDragMode property of the source control to 1 (Automatic) and the OLEDropMode of the destination control to 2 (Automatic),

It shows the OLEDDAUTO project (you’ll find it in the Oledd folder on the CD), whose main Form contains a RichTextBox control, a TextBox control. and a PictureBoxcontrol, Click the Load Image button to load an image to the PictureBox control and enter some text in the TextBox and RichTextBox controls. You can also open a text file in the TextBox control by clicking the Load Text File button.

If you drag the image and drop it on the RichTextBox control, it will be inserted in the text at the pointer’s location. You can also select part of the text in the Text- Box control and drop’ it to the RichTextBox control. It will be inserted at the current location. As you can see, by using the Automatic setting for the OLEDragMode and OLEDropMode properties, you can bring OLE drag-and-drop operations into your application with no coding at all.

Obviously, the destination of an OLE drag-and-drop operation must be able to recognize and display multiple types of information, and in this sense, the Rich- TextBox control is an ideal candidate. This application also demonstrates a very important property of the RichTexBox control: OLE Objects. The RichTextBox control’s OLEObjects property is a collection that contains all the objects in the control except for text. Drop an image or two on the RichTextBox control of the OLEDDAUT9 application. Then drop a few files from ‘the Desktop or any other folder. Just make sure that the application and the folder’s window with the desired file are visible, then drag a file’s icon and drop it on the RichTextBox control. To drop the text of the TextBox control, you must first select it with the mouse, then drag it across to the RichTextBox control.

After you’ve placed a few objects on the RichTextBox control, click the List All Object files button and you’ll see a description of each object in the control. The code behind the Command button scans the OLEObjects collection and prints out the Class property of -each item in the collection in the Immediate window. The Class of a bitmap is StaticDIB.

The items of the OLEObjects collection have other properties too, such as the DisplayType property (which determines whether an object appears on the control as contents or as an icon) and the ObjectVerbs collection (contains all the verbs, or commands, recognized by the object). To find out which actions an embedded object can carry out, you must first call the Fetch Verbs method as follows;

To invoke a specific verb (like the Play verb for a multimedia file) on one of the objects, use the following statement:

RichTextBoxl.OLEObjects(1).DoVerb(“Play”)

Scroll to Top