Dragging and Dropping

Posted on 1999-07-01
Medium Priority
Last Modified: 2010-05-02
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?
Question by:mi5
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

DavidMartin earned 100 total points
ID: 1521275
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

Expert Comment

ID: 1521276
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


Expert Comment

ID: 1521277
vindevogel, that sounds familiar! :}

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question