Browse DevX
Sign up for e-mail newsletters from DevX


Using OLE Drag-and-Drop in Visual Basic 6 : Page 5

There are two types of Drag-and-Drop functionality in Visual Basic: the traditional kind and OLE. This second type of drag-and-drop allows users to drag data between applications as well as within an application. However, it only works with applications that support it. Luckily, this includes most Microsoft applications and many programs from other vendors as well. In this article, learn how to use this basic but essential functionality.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Putting It All Together
Now let's look at the basic steps required to implement manual OLE drag-and-drop for Visual Basic controls. There are two things to consider: a control that serves as a source, and a control that serves as a target. Some controls can do both, of course. In either case, the control's OLEDragMode and OLEDropMode properties must be set to Manual. For a target control:

  1. Place code in the control's OLEDragOver event procedure that checks the format of the data in the DataObject object. Depending on whether the available format or formats are appropriate for the control, set the effect argument to the appropriate value (vbDropEffectNone, vbDropEffectCopy, or vbDropEffectMove). This causes the mouse cursor to display the appropriate symbol while dragging over the target control.
  2. Place code in the control's OLEDragDrop event procedure to check the format of the DataObject's data. The code can also check the state of the keyboard to see if the Ctrl key is depressed indicating a Copy operation rather than a move. If the format of the data is acceptable, and depending on other factors such as the state of the keyboard, set the effect argument to the appropriate action (vbDropEffectNone, vbDropEffectCopy, or vbDropEffectMove) and retrieve the data from the DataObject.

When a control is acting as an OLE drag-and-drop source here's what is required:

  1. In the control's MouseMove event procedure, check the state of the mouse buttons and if the left button is depressed initiate an OLE drag-and-drop operation by calling the control's OLEDrag method.
  2. In the control's OLEStartDrag event procedure, which is called as soon as the OLEDrag method is executed, insert the data from the control into the DataObject object using the SetData method (although this step can be deferred, as I'll explain soon). If necessary, call SetData additional times to specify additional data formats. Also, set the AllowedEffects argument as appropriate.
  3. Optionally, put code in the control's OLEGiveFeedback event procedure to provide visual feedback as the data is dragged over various parts of the screen. Information set in a target's OLEDragOver event procedure, specifically the effects argument, is available in the OLEGiveFeedback event procedure. Most often this event procedure is used to change the appearance of the source control depending on where it is being dragged.
  4. In the control's OLECompleteDrag event procedure place any code that is required to complete the operation. Typically this consists of erasing the original data from the control after a move operation. For a copy operation this event procedure is usually not used.

A Demonstration
The following demonstration program puts these techniques into action. While this is a simple application, it demonstrates all of the techniques you'll need to implement full-featured OLE drag-and-drop in your Visual Basic programs. To create the application, start a new Visual Basic project and place two Text Box controls and one Picture Box control on its form. Set the OLEDropMode and OLEDragMode properties to Manual for each of these controls. Here's how the program works:

Figure 1 shows the program after an image was dragged from another program to the Picture Box control.

  • You can drag from the first Text Box to copy its data to a target such as the second Text Box or another program that supports OLE drag-and-drop of text, such as Microsoft Word. If you drag the first Text Box over the form or Picture Box, or anywhere else where a drop is not permitted, the text in the Text Box changes to red.
  • The second Text Box operates the same as the first Text Box except you can specify whether the data is to be copied (by depressing Ctrl) or moved (no keys depressed).
  • Bitmap data from another program, such as PhotoShop, can be dragged to the Picture Box. If bitmap data is dragged over either of the Text Box controls, the "no drop" icon is displayed.

Listing 1 shows the code for this demo's event procedures. Each procedure is commented so you can relate it to the explanations in the text. Figure 1 shows the program after an image was dragged from another program to the Picture Box control.

Peter G. Aitken has been writing about computers and programming for over 10 years, with some 30 books and hundreds of articles to his credit. Recent book titles include Developing Office Solutions With Office 2000 Components and VBA, Windows Script Host, and the soon to be published XML the Microsoft Way. He is a regular contributor to OfficePro magazine, and for several years was a contributing editor for Visual Developer Magazine where he wrote the popular Visual Basic column. Peter is the proprietor of PGA Consulting, providing custom application and Internet development to business, academia, and government since 1994. You can reach him at peter@pgacon.com.
Thanks for your registration, follow us on our social networks to keep up-to-date