Drag n' Drop

Download a sample VB program that performs drag and drop.

Instructions for Creating the Example:

  1. Start Visual Basic from the Start menu.
  2. Select Standard EXE, and click Open.
  3. Save this module to your program's directory -> mMove.bas
  4. Add the mMove module to your project by clicking on the add new Module icon and choosing Existing File.

  1. Place a timer on your form and name it movingTimer.
  2. Add the following code to the timer:

Private Sub movingTimer_Timer()
    mMove.ToNewPosition
End Sub

  1. In Form_Load add this line of code: 

mMove.Initialize movingTimer ' where MovingTimer is the name of the timer on the form

  1. Add 4 picture boxes to your form.  Create a control array of 3 of them named picObject. (see Control Array example if you don't know how to make a control array.)
  1. Name the fourth picture box picTrash.  You can place a picture in the trash can picture box if you like (sample picture: trashcan.gif)

  1. For the object's you want to move, you must place code in their MouseDown, MouseMove, and MouseUp subroutines that uses code in mMove.  Place the following code in picObject's mouse events:

Private Sub picObject_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' start movement of object
    If Button = 1 Then mMove.Begin Me.ActiveControl, X, Y

End Sub

Private Sub picObject_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' move object
    If Button = 1 Then mMove.InProgress X, Y
End Sub

Private Sub picObject_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' stop moving object
    If Button = 1 Then mMove.EndIt
End Sub

 

  1. Create a function that will check if an object is over top of the trash can when it is being moved.  You can type this at the bottom of your code.

Public Function checkTrashCollision(X As Integer, Y As Integer) As Boolean
    ' check if top left corner is inside the trash can
    If X > picTrash.Left And X < picTrash.Left + picTrash.Width Then
        If Y > picTrash.Top And Y < picTrash.Top + picTrash.Height Then
            checkTrashCollision = True
        End If
    End If
End Function

 

  1. Add this line to picObject's mouse down event so that when an object moves it is on top of all the other objects:

' bring on top of all other objects
picObject(Index).ZOrder

 

  1. Lastly, call our collision checking function when picObject is being moved, your MouseMove subroutine will now look like this:

Private Sub picObject_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
    ' move object
    If Button = 1 Then mMove.InProgress X, Y

    ' see if the top left corner of the object is inside the trash can
    If checkTrashCollision(picObject(Index).Left, picObject(Index).Top) = True Then
        ' make the object disappear
        picObject(Index).Visible = False
    End If
End Sub