Posted on 2006-05-21
Last Modified: 2010-04-07
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
Question by:const71
    LVL 1

    Accepted Solution

    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

    Author Comment

    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?

    Author Comment


    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?
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    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

    Author Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now