Detecting End of Drag & Drops Sequences

Microsoft in their invariable wisdom somehow chose to make the manual DRAG function operate rather counter-intuitively. That is, when an item is dropped, the Mouse_Up function for the initiating subroutine is NOT called! This makes it very difficult to detect unsuccessful drops. Detecting an unsuccessful drop can be of importance when you need to update animation that may be in progress to support the Drag-Drop Sequence. To this end, this application note will suggest a very simple method for detecting the End_Of_Drag sequence. This technique work whether the operation was successful or not. One of its major benefits of this technique is that it does not require any custom control programming!

The method involves generating the proper Mouse_Up event at the end of the drag operation. This can be accomplished using a single Windows API call and a normal Visual Basic Timer. The sequence is:

  1. Enable the timer in the Mouse_Down sequence after any animation steps but just before starting the manual Drag sequence.
  2. On each timer tick look at the mouse button to determine if the operator has released.

The steps are pretty simple:

'**** In a .BAS form (as you will probably use this elsewhere) 
Declare the API Function Declare Function GetKeyState Lib "User" (ByVal nKey As Integer) As Integer
'**** In the code sequence for the control of Interest 
Sub Text2_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
    FileCabinet = FileOpenIcon ' *** Animation, transfer image from one Box to another 
    Text2.DragIcon = AnyIcon ' *** Set up the Drag Icon
    Timer1.Enabled = True ' *** Enable the Timer (Put on the form at Design Time)
    Text2.Drag 1 '*** Start the manual Drag Operation
End Sub 
'***** In the form Timer Event Code Section 
Sub Timer1_Timer ()
    K% = 1 '*** Set the Virtual Scan code for the Mouse Left Button 
    I = GetKeyState(K%) '*** Get the Key state
    I = I And (Not 1) '*** Mask off the unimportant Bits
    If I = 0 Then '*** Check to see if the operator has released the button
        FileCabinet = FileClosed '*** Finish animation, by transferring images
        Timer1.Enabled = 0 '*** Turn off the timer so this event will not be triggered
    End If
End Sub


Tip Submitted By: Michael D. Strathman