Drag & Drop
You see it in just about every Windows application, so how can you do it in
your VB application? Fortunately VB provides built-in tools to make it easy
for you. While the code you write to respond to a drag and drop operation
can be fairly complex, the mechanics of a drag and drop operation are
fairly simple and logical. VB especially handles well the graphical
tasks of a drag and drop operation - creating and moving the icons displayed
during a drag and drop operation.
Drag and Drop
Ok, a short description first. Put the mouse cursor over an object on a
form, then press and hold the left mouse button. Without releasing the
button, move the mouse cursor to a new location. Then release the mouse
button.
Was that fun? Either way, what you just did was a drag and drop operation.
How your program responded to what you did was entirely up to the programmer
of the application. In programs which support drag and drop operations,
the cursor changes to an icon (indicating a drag operation is taking place)
and when the mouse is released the icon reverts back to a normal cursor
(indicating that the drop operation is now over).
VB offers both an automated and a manual way to allow users to perform a
drag and drop operation. In either case, just like
with common dialog windows, a drap and drop operation is just a method
of having a user give you instructions/data, which you must use in code
before anything happens.
The visible, outward sign of a drag and drop operation is the appearance
and then disappearance of an icon during the operation. However, if you
don't have code in the appropriate event, absolutely nothing else will
take place in your application.
Drag and Drop Events
By way of clarification, a drag and drop event takes places between two
objects. At one object a drag is initiated and at the second object a drop
takes place.
When the drag begins, the first object's does not experience an event. VB
silently handles the creation and movement of the drag icon. When the
mouse is released, signalling a drop operation, the DragDrop event of the
receiving control is fired and any code in that event is executed. If the
DragDrop event has no code, then nothing else happens.
A second event is possible, which is the DragOver event. After a drag is
initiated and at the moment the drag icon is moved over another control,
that control's DragOver event is fired. It's only when the mouse button
is released that the DragDrop event takes place on the receiving control.
The dragdrop can be very useful, users know how to use it and users see
it in most of the
popular shrinkwrap applications. With that background, how can you not put
it into your own application? Well, first of all, not every application can
be made easier to use by simply tossing in some drag and drop features. In
my own experience, I use drag and drop in only 1-2 out of ten programs I
write. It has to add significant value to be worth the time it takes to
program.
There are only two event to worry about, the DragDrop and the DragOver,
so it won't take long to cover them both. In this example, the events of
a form (Form1) are examined:
Private Sub Form1_DragDrop(Source As Control, _
X As Single, Y As Single)
Private Sub Form1_DragOver(Source As Control, _
X As Single, Y As Single, State As Integer)
Both events share the common arguments Source, X, and Y, whereas the
DragOver events includes the additional argument State.
- Source
This is a reference to the control which is being dragged.
For example, if you drag a command button onto a form, Source refers to the
command button. You would use Source.caption to refer to the caption of
the command button. You can access other properties or methods of the
command button the same way, by using the argument in place of the actual
command button's name.
- X/Y
X and Y are the position at which the drop took place. You may use that
information in a variety of ways, but if it is used at all, it is typically
used to reposition the control from which the drag and drop operation began.
- State
An integer value corresponding to whether the dragged object is entering or
leaving the control or has simply moved its location within the control.
Drag and Drop Properties
The only two properties of a control which affect a drag and drop operation
are:
- DragMode
There are two settings - automatic or manual. If automatic is chosen, then
all you do is drag a control (hold down the mouse while moving the cursor)
and VB will automatically create the drag icon. If manual is selected, then
you must invoke the .Drag method of the control for to begin the drag
operation (including the display of the drag icon).
- DragIcon
VB provides a default icon (a small outline image) but you may also define
the icon to be used while the drag is taking place.
Drag and Drop Methods
- Drag
The only method needed is appropriately called Drag. To use it, simply use
code such as:
command1.drag
This will initiate a drag operation but as in the automatic case you still
must write any code that is executed when the drop action occurs.
|