ne major advantage of the Windows operating systemand most other operating systems for that matteris that it provides all programs with a similar user interface. A program's menu, for example, is always displayed across the top of its window, and its commands can be accessed with the mouse or by using the Alt key. Pressing Alt+F4 closes a program, F1 is almost always the Help key, and Ctrl+P usually is the command for printing. These and other cross-program similarities in user interface commands make the user's life a lot easier. Imagine having to learn a new interface and all new commands for each program!
One important aspect of the Windows interface that many developers overlook is drag-and-drop. The ability to carry out program operations using drag-and-drop can be a significant part of a user interface, particularly for those users who are happier using the mouse rather than the keyboard. Implementing drag-and-drop in a Visual Basic 6 program is a relatively simple task. This article covers only traditional drag-and-drop, which permits items to be dragged from one part of a program to another part of the same program. There is a second kind of drag-and-drop that permits items to be dragged from one program to another program, which is not covered here.
The Basics of Drag and Drop
A drag-and-drop operation involves a source and a target. The source can be any Visual Basic control, and the target can be any control or the form itself. The operation has three parts:
- The operation begins when the user presses the mouse button as the pointer is over a control that has been enabled as a drag-and-drop source.
- The operation continues as the pointer is moved, with the left mouse button still down, over other controls or the form itself. A control receives notification, via the DragOver event, that it is being "dragged over" and can signal whether the data can be dropped on it by changing the appearance of the mouse pointer.
- The operation ends when the user releases the mouse button. A control is notified by the DragDrop event that it has been dropped on.
It's important to note that what is being dragged is the source control. It's not being dragged literallyit does not move on the formbut its identity is being dragged. Both the DragOver and DragDrop events inform the target control of the identity of the source control, and code in the event procedure can take action accordingly.