Drag and drop is everywhere in the Windows operating environment. You can drag files to and from Windows Explorer, the desktop, folders, and icons and shortcuts representing other applications. For example, if you drag a text file’s icon from Windows Explorer onto a Notepad shortcut on your desktop, the Notepad program opens and displays the text file.
While common throughout Windows applications in general, drag and drop is rare in Visual Basic programs. This is not because it is difficult, but because it is poorly understood. This article explains two ways you can add drag and drop to your Visual Basic programs. The following sections explain how to respond when files are dragged onto a program’s icon and when objects are dragged within a single program.
Drop Start
There are many ways to start a program. One method is to drag objects such as files onto the program’s icon in Windows Explorer or on the desktop. When you do this, the program starts, receiving the full path names of the dragged files as command line parameters.A Visual Basic program can use the Command$ function to learn its command line parameters and take appropriate action. For example, an editor program might open the files and display them. A printing program might print them.
Program Commands reads its command line arguments and displays them on separate lines using the following code:
‘ Display the command line arguments. |
Because this technique is so easy, you should make any program that displays or processes files use Command$ to allow this type of drop start.
Drag & Drop
Most Visual Basic controls provide the ability to drag and drop within a program. If you set a control’s DragMode property to 1 – Automatic, that control automatically begins dragging when the user clicks and drags on it. If you leave DragMode set to 0 – Manual, you can start a drag for the control by executing its Drag method. Some controls, including the TextBox, do not have a DragMode property and using their Drag method is the only way to start a drag.When the user drags across another control, that control receives a DragOver event. The event handler receives as a parameter a reference to the source control being dragged. The destination control can examine the properties of the source control to provide visual feedback. For example, the control can examine the source control’s name and, if the destination control understands how to perform a drop from the source, it can highlight itself to show that it is ready for a drop.
Finally, when the user releases a drag over a control, that control receives a DragDrop event. Like the DragOver event handler, DragDrop receives a reference to the source control. It can examine the source and use its properties to complete the drop. For instance, a PictureBox might verify that the source control is another PictureBox and copy its picture.
Program Drag demonstrates drag and drop. It allows you to drag pictures from a control array of picDragFrom PictureBoxes into a control array of picDragTo PictureBoxes.
When you drag over the form or one of the picDragFrom pictures, the corresponding DragOver event handler sets the source control’s DragIcon property to a “no drop” cursor. That makes it obvious to the user that a drop is not allowed. Visual Basic automatically displays a “no drop” cursor when you drag off of the application.
When you drag over a picDragTo control, the control verifies that the drag source control has name picDragFrom. If it does, the control changes the source control’s DragIcon property to a “drag ok” cursor so it is obvious that a drop is allowed.
When you release the drag above a picDragTo control, the control’s DragDrop event handler copies the source control’s picture. Because the form and picDragFrom controls do not have DragDrop event handlers, they ignore drop events.
The following code shows how program Drag works.
Option Explicit |
Your Second Drag
Using the techniques described in Part I, you can add drag start and drag within a program. Now that you have these skills be sure to check out Part Two, to see how you can drag between programs in your Visual Basic applications!
Rod Stephens is a former senior member of GTE’s technical staff. A programmer for over 13 years, Rod has designed and implemented more than a half dozen award-winning commercial applications, and author over 80 articles and seven books on Visual Basic Programming. Be sure to visit his VB education site, VB-Helper, at www.vb-helper.com!