Drag and Drop Operations
When a user drags one object from one location to
another, your application needs to know about it.
Drag-and-drop is a process where the user, with
the mouse, clicks an object, holds down the mouse
button, and drags that object from one location
to another location. |
VB supports two kinds of drag-and-drop operations:
The Automatic drag-n-drop is the simplest. The form's
DragDrop event controls the placement of the drop.
To set up the drag, you only need to change the control's
DragMode property to 1-Automatic. When the user moves
the control, VB displays an outline of the control,
but it doesn't actually move the object. The following
code performs automatic drag-and-drop:
Private Sub frmDemo_DragDrop (Source
As Control, _
X As Single, Y As Single)
' the code receives the actual control
' that the user dragged as an argument
'move the control to the dropped
location
Source.Move X, Y
End Sub
The Move method moves
the control from its original location to the desired
location (where the user releases the mouse button).
The manual drag-n-drop works like the automatic drag-n-drop,
with the following differences:
- You must set the DragMode property to 0-Manual
- The Manual setting also allows you to recognize
a MouseDown event before dragging starts, so that
you can record the mouse position.
- You must add code to the mouse Down event procedure
to invoke the drag.
To enable dragging from code, leave DragMode in its
default setting (0-Manual). Then use the Drag method
whenever you want to begin or stop dragging an object.
Use the following Visual Basic constants to specify
the action of the Drag argument.
Constant |
Value |
Meaning |
vbCancel |
0 |
Cancel drag
operation |
vbBeginDrag |
1 |
Begin drag operation
|
vbEndDrag |
2 |
End drag operation
|
The MouseDown event procedure can perform the special
Drag method on the object is you want to continue
the drag-n-drop operation.
Private Sub Image1_MouseDown(Button
As Integer, _
Shift As Integer, X As Single, Y As Single)
txtStore.Text = "Clicked over
the image at " & X & ", " &
Y
Image1.Drag
End Sub
The Drag method initiates dragging of the control.
|
Use the manual drag-and-drop operations
if you want to set up drag-and-drop limitations
before and during the drag-and-drop process. |
|
|
One of the foundations of Windows
applications is that they respond to the mouse.
Today's lesson explained how to integrate the
mouse into your Visual Basic application. The
mouse events inform your program of clicks,
double clicks, and the buttons pressed. Moreover,
we explained how to implement drag-and-drop
operations.
|
|