• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Dragging and Dropping

How can I drop an item onto a form when the user has dragged it. For a control such as a picture, I use picture1.drag to make the shadow of the box follow the mouse. I want to make the object stay where the user clicks the mouse. I have tried using picture1.left = X and picture1.top = Y (where X and Y are the mouse pointer coordinates), but the object will move because the X and Y coordinates are never in the same place as where the x and y coordinates of the box are measured. Is there an easy command to drop the object?
  • 2
1 Solution
Try storing the position of the mouse on the object then taking this away from the final position

Dim mX As Single
Dim mY As Single

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Picture1.Move X - mX, Y - mY
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Picture1.Drag vbBeginDrag
    mX = X
    mY = Y
End Sub
On your mouse down event in the item you drag, you also have an X and Y (say DownX and DownY)
These are the values where the mouse click occurs

The drop generates an X and Y too, being the ones where the drop occurs (say DropX, DropY)

So, where you want to set the left, you should set it to
Left = DropX - DownX
(Same for Y)

This way, you're control will stick to the place where you drop it

vindevogel, that sounds familiar! :}

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now