Drag and Drop Tutorial

Cogen
09-27-2002, 03:12 PM
This is a little drag and drop tutorial for intermediate
programmers with little or no drag and drop experience.
Most intermediate programmers should have no problem
following these topics, as they are not that advanced.
I might expand on this eventually with some more advanced
topics, but this is enough to get you started.

The first thing in learning how to do basic dragging and dropping
is understanding mouse events. There are 3 mouse events ...
MouseUp, MouseDown, and MouseMove. They are pretty self
explanatory, but just to clarify, the MouseDown fires when the
user presses a mouse button down, the MouseUp fires when the user releases the mouse button, and the MouseMove fires whenever the mouse moves location across the screen. Each of these events have the same parameters ...

_MouseUp(button As Integer, shift As Integer, _
x As Single, y As Single).

-The "button" refers to which button is clicked
(VbLeftButton, VbRightButton, VbMiddleButton).
-The shift refers to whether the shift, alt, or ctrl keys are
pressed(vbShiftMask, vbCtrlMask, vbAltMask).
-The X and Y values refer to the coordinates of the mouse
relative to the upper left corner of the control object
receiving the event.


Normal Drag and Drop
-----------------------
By normal drag and drop, I'm mainly referring to the
functionality of the DragMode property, which allows the entire
object to be moved. I'll also briefly discuss OLE Drag and Drop
below, which allows for data inside objects (text and graphics)
to have drag and drop functionality, while the actual object
stays in one place.

To perform normal dragging and dropping, it can be as simple as
setting the objects (controls) DragMode property to automatic.
You can do this at design time or run time ...

Picture1.DragMode = vbAutomatic

It's important to note that this will allow you to drag your
control, however once you release the mouse button, nothing will
happen, because you haven't specified any dropping code. When using an automatic DragMode, the control being dragged will not receive mouse events, which is why I suggest using the manual approach. Before explaining why, I'll show you how to drop the control that you just dragged. All you need to do is determine what your possible target/s is, and place some code in it's DragDrop event. The DragDrop event has 3 parameters
(Source,x,y). Source is the Control that's being dropped on.
X and Y is the location of the drop. Therefore, if I wanted to
move the above control that's being dragged somewhere else on the form, I could place the following code in the forms
DragDrop event.

Private Sub Form_DragDrop(Source As Control, _
X As Single, Y As Single)
Source.Move X, Y
End Sub

Now you might be asking "why bother doing it manually when
the automatic does dragging and dropping so easily?". The
reason is that doing it manually is also pretty easy, but
allows for more functionality since it doesn't stop your
control from firing mouse events.

To do the same thing manually you can set your DragMode to
manual.
This can be done at run time ...

Picture1.DragMode = vbManual

but I would suggest doing it at design time.
Since a manual DragMode doesn't do anything by it self, you
are required to write some code in the objects MouseDown
event ...

Private Sub Picture1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
Picture1.Drag vbBeginDrag
End Sub

That's all the code required to get your control to drag
manually. If you want to drop the control in the same
relative position to the pointer as when you clicked on it to
drag it, you need to store the x and y coordinates in the mouse
down event and use them as an offset when you drop them ...

Private XOffset As Integer
Private YOffset As Integer

Private Sub Picture2_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
XOffset = X
YOffset = Y
Picture2.Drag vbBeginDrag
End Sub

Private Sub Form_DragDrop(Source As Control, X As Single, _
Y As Single)
Source.Move (X - XOffset), (Y - YOffset)
End Sub



OLE Drag and Drop
-----------------------
This is a little more advanced topic then the normal drag
and drop, so I'm not going to go into very much detail. Unlike
normal drag and drop, OLE drag and drop allows you to move data (text and graphics) from one control to another. This style
of Drag and Drop also has an automatic and manual method,
although not all controls support automatic. Some controls
will support automatic dragging but not dropping. To figure
out what a control supports, use the Property Window at design
time and look at the options available for OLEDragMode and
OLEDropMode. When either of these properties is set to
automatic, no code is required to get it to work, however
when using the manual method (by choice or because automatic
isn't supported), you must use various OLE drag and drop
properties, methods, and events. This is beyond my little
tutorial, but I'm sure you can find information about them in
msdn or google.com.
For an example of OLE drag and drop, open a standard form and
place two picture boxes on it. Put a .bmp or .gif in one of
the boxes, and then set the OLEDragMode and OLEDropMode to
automatic for both of the picture boxes. Now you can run the
program and drag the picture from one box to the other. Try
holding down the control key while doing it!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum