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

HOW CAN A USER CANCEL A DRAG AND DROP OPERATION IN VISUAL BASIC?

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
0
const71
Asked:
const71
  • 3
2 Solutions
 
BrownheadCommented:
Is this what your looking for?

'//Code\\
'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
    DoEvents
Loop
Image1.Drag 0
End Sub
'\\Code//
0
 
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?
0
 
const71Author Commented:
yep,

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?
0
 
Mike TomlinsonMiddle 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
0
 
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
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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