mouse on the control.
DragIcon. Specifies the mouse pointer that is displayed while the control is being dragged. The defaultsetting displays an arrow with a rectangle. For a custom mouse icon, set this property at design-time toa .ICO or .CUR file, or at run-time use the LoadPicture function to load an .ICO file.Executing the Drag method on the source control is required only when the its DragMode property is set tovbManual. The syntax for this method is:
to vbBeginDrag (value = 1) to initiate a drag operation. This will usually be done in the sourcecontrol's MouseDown event procedure. You can also call the Drag method with
set to vbCancel or vbEndDrag (values 0 and 2 respectively) to cancel an ongoing drag-and-drop operation or to end a drag-and-drop operation.Controls have two events that are related to drag-and-drop. DragOver is used to detect when an object isdragged over a control, and DragDrop is used to detect when an object is dropped on a control:
target_DragOver(source As Control, x As Single, y As Single, State As Integer)target_DragDrop(source As Control, x As Single, y As Single)
identifies the target object (the one being dragged over or dropped on). It can be a form, an MDI form,or a control. If the target is a control that is part of a control array, these event procedures will have anadditional argument that specifies the Index property of the control within the control array.
identifies the source control (where the drag-drop operation began).
give the horizontal and vertical position of the mouse pointer with respect to
. These values arealways expressed according to the object's coordinate system.
specifies the relationship between the mouse pointer and the target, as follows:
A value of 0 indicates the pointer just entered the target.
A value of 1 indicates the pointer is leaving the target.
A value of 2 indicates that the pointer is moving within the target.When a source control is dragged and dropped, here's what happens:1.When the mouse pointer leaves the source control, the parent form receives a single DragOver eventwith the State argument equal to 0.2.As the pointer moves over the form the form receives multiple DragOver events with the Stateargument equal to 2.3.When the source is dragged over another control on the form, the form receives a DragOver event withthe State argument equal to 1 (signaling that the pointer has left the form), and the control over whichthe source was just dragged receives a DragOver event with the State argument equal to 0 (signalingthat the pointer has entered the Form).4.When the control is dropped, the object it is currently over receives a DragDrop event.Let's look at some examples, starting with something really simple. Create a Visual Basic project and place aText Box and a Label on the form. Set the Text Box's DragMode property to vbAutomatic. Put the followingcode in the Label's DragDrop event procedure:
Private Sub Label1_DragDrop(Source As Control, _X As Single, Y As Single)Label1.Caption = Source.TextEnd Sub
When you run the project, enter some text in the Text Box then drag from the Text Box to the Label. Doing
Página 2 de 4Implementing Drag-and-Drop in Visual Basic 601/02/2009http://www.devx.com/vb/Article/8029/1954