Programming OLE Drag-and-Drop Operations VB Help

Manual OLE drag-and-drop operations are more flexible than automatic operations because your program is in control. The OLEDDMAN project (also in the OLEDD folder on the CD) has the same interface as the OLEDDAUTO project, but it uses the ‘manual setting of the OLEDropMode property. The TextBox’s OLEDragMode property is set to Automatic, so no code is needed to initiate the operation. The same is true for the PictureBox and RichTexBox controls .

Each control detects when an object is dropped on it and they react to the action differently. Let’s start with the simpler control, the TextBox control. When an object is dropped, the OLEDragDrop event takes place, which is equivalent to the Drag- Drop event. The OLEDragDrop event’s definition is:

Data is an object .variable that represents the object that was dropped. The Data variable provides several properties for accessing the object (they are discussed shortly). Effect is a constant that determines the type of the drop (whether it’s a Move or Copy operation). The Button argument reports the button that triggered the drop operation; the Shift argument contains information about the status of the Shift, Alt, and Ctrl keys; and the last two arguments are the coordinates of the point where the drop operation took place.

The methods of the Data object are similar to the methods of the Clipboard object, The GetFormat method lets you find out the format of the data. The GetFormat method doesn’t return a data type; instead,’ it must be called with a data type, and it returns True if the data is of the specified type, False otherwise. To find out whether the Data object contains text, use an If structure like the following one:

(The constants that describe the various data types are the same ones used with the Clipboard.)

To actually retrieve the data, use the GetData method. The GetData method returns an object, which must be assigned to the proper variable. If the data format is text, you can assign the value returned by the GetData method to the Text property of a TextBox control. If the data is a bitmap, you can assign it to the Picture property of a console that can display images.

where format is a constant that determines the desired data format. Many times, the same object can have different formats. When you select a paragraph from a Word document, you can drop it on a TextBox control as text or you can drop it on a Rich- TextBox control as formatted text. The data itself can be retrieved in multiple formats, depending on the capabilities of the destination control. The EditCopy method . . of the Graph control (discussed in Appendix B, The MSChart Control has a similar behavior. The EditCopy method copies the current graph from a Graph control. The same data can be pasted as a bitmap on an image processing application or as text on a text editor.

If the user has dropped one or more files on the control (the expression Get- Format (vbCFFiles) is True), you can us~ the Files collection of the Data object to retrieve their names:

Here’s how the TextBox control handles OLE drag-and-drop operations:

If you drop one or more files on the TextBox control, their names will be listed. If you drop the RichTextBox control’s text, then the same text (without formatting) will appear on the TextBox control. Finally, if you drop the image of the PictureBox control, then the image’s characteristics will be displayed. Notice how the program . retrieves the dimensions of the image. The object returned by the GetData method is a Picture object; therefore, we can call its Width and Height properties to read its .dimensions.

The code in the RichTextBox control’s OLEDragDrop event handler is similar, The . RichTextBox control understands more types of data and it must handle each one differently. You can open the project in Visual Basic’s IDE and examine the code. If you attempt to drop the image on the RichTextBox control, you’ll get an error message. The following statements should insert the bitmap in the RichTextBox control, but they fail. Instead of moving the image, they produce a runtime error.

The only workaround I’m able to suggest is to momentarily switch the Rich- TextBox control’s OLEDropMode property to Automatic, then reset it to manual. The first action must take place from within the PictureBox control’s OLEStart-
Drag event:

After the drop operation completes, you can reset the RichTextBox control’s OLEDropMode property back to manual. This action must take place in the Text- Box control’s OLECompleteDrag event, which signals the end of the OLE drag and- drop operation:

OLE drag-and-drop operations are similar to plain drag-and-drop operations, but they can get quite a bit metre complicated because of the variety of objects that can be dropped. The destination control must be able to decipher the type of object that is dropped and handle it accordingly. The ability to detect the drop of files on a Visual Basic Form means that you can write applications that can interact with the Desktop (receive the files dropped by the user, process them, move them to a different folder, and so on). OLE drag-and-drop is a very interesting capability which you may wish to further explore on your own. See the Visual Basic Help files for more information on related properties and events.

Posted on November 6, 2015 in OLE Automation and VBA

Share the Story

Back to Top
Share This