I have set my control for manual drag and drop,

in the mouse move event, i check to see if the left mouse button is down and immediately call Drag method to initiate a drag and drop operation.

While the user is dragging the control, how do i code it so that if the user clicks the escape key (or perhaps the right mouse button), the drag and drop is cancelled completely (ie no further DragOver or DragDrop event will be called until I once again initiate the whole sequence over with another call to Drag

During drag and drop all mouse events seem to be turned off untill the end of the drag and drop operation so i am not even sure if i can cancel the drag and drop with a right mouse click. --If it is too difficult, I will settle for the escape key :)

Hope this makes sense
Who is Participating?
BrownheadConnect With a Mentor Commented:
Is this what your looking for?

'Code Requires 1 Image box with drag mode set to manual
Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Image1.Drag 'Begin Dragging
Do Until GetAsyncKeyState(2) And &HF0000000 'Check For Right Click
Image1.Drag 0
End Sub
const71Author Commented:
That seems to work for the mouse button but not for escape key. Also, what if the right mouse is never clicked and the user always performs drag and drop without ever pressing right mouse?  will this  loop forever?
const71Author Commented:

i put a beep in the loop and my program can't finish until it has exhausted all beeps yet to be played.
Isnt there a cleaner way to do this?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Try this out...

Create a Project with just a Timer control and a PictureBox:

Option Explicit

Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Private xOffset As Single
Private yOffset As Single

Private Sub Form_Load()
    Me.KeyPreview = True
    Timer1.Interval = 75
End Sub

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

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    If Source Is Picture1 Then
        Picture1.Move X - xOffset, Y - yOffset
    End If
End Sub

Private Sub Timer1_Timer()
    If IsKeyDown(vbKeyEscape) <> 0 Then
        Picture1.Drag vbCancel
    End If
End Sub

Private Function IsKeyDown(ByVal key As Long) As Boolean
    Const KeyDownMask As Long = 32768
    Dim state As Long
    state = GetKeyState(key)
    IsKeyDown = ((state And KeyDownMask) > 0)
End Function
const71Author Commented:
Great code guys!

The use of timer is probably not the way to go in my opinion as the user will only realize their drag was canceled when the Timer interval is reached. Shorten that interval too much and you're wasting cycles.

But the code did give me some ideas about keyboard manipulation which I need.  I guess first approach with some tweaks will work best.

Thanks guys
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.