May 26, 2018
Hot Topics:

Visual Basic Drag & Drop, Part One

  • March 16, 1999
  • By Rod Stephens
  • Send Email »
  • More Articles »

Drag and drop is everywhere in the Windows operating environment. You can drag files to and from Windows Explorer, the desktop, folders, and icons and shortcuts representing other applications. For example, if you drag a text file's icon from Windows Explorer onto a Notepad shortcut on your desktop, the Notepad program opens and displays the text file.

While common throughout Windows applications in general, drag and drop is rare in Visual Basic programs. This is not because it is difficult, but because it is poorly understood. This article explains two ways you can add drag and drop to your Visual Basic programs. The following sections explain how to respond when files are dragged onto a program's icon and when objects are dragged within a single program.

Drop Start

There are many ways to start a program. One method is to drag objects such as files onto the program's icon in Windows Explorer or on the desktop. When you do this, the program starts, receiving the full path names of the dragged files as command line parameters.

A Visual Basic program can use the Command$ function to learn its command line parameters and take appropriate action. For example, an editor program might open the files and display them. A printing program might print them.

Program Commands reads its command line arguments and displays them on separate lines using the following code:

' Display the command line arguments.
Private Sub Form_Load()
Dim cmds As String
Dim txt As String
Dim pos As Integer

    cmds = Command$
    Do While Len(cmds) > 0
        pos = InStr(cmds, " ")
        If pos = 0 Then
            txt = txt & cmds
            cmds = ""
            txt = txt & Left$(cmds, pos - 1) & vbCrLf
            cmds = Mid$(cmds, pos + 1)
        End If
    lblArguments.Caption = txt
End Sub

Because this technique is so easy, you should make any program that displays or processes files use Command$ to allow this type of drop start.

Drag & Drop

Most Visual Basic controls provide the ability to drag and drop within a program. If you set a control's DragMode property to 1 - Automatic, that control automatically begins dragging when the user clicks and drags on it. If you leave DragMode set to 0 - Manual, you can start a drag for the control by executing its Drag method. Some controls, including the TextBox, do not have a DragMode property and using their Drag method is the only way to start a drag.

When the user drags across another control, that control receives a DragOver event. The event handler receives as a parameter a reference to the source control being dragged. The destination control can examine the properties of the source control to provide visual feedback. For example, the control can examine the source control's name and, if the destination control understands how to perform a drop from the source, it can highlight itself to show that it is ready for a drop.

Finally, when the user releases a drag over a control, that control receives a DragDrop event. Like the DragOver event handler, DragDrop receives a reference to the source control. It can examine the source and use its properties to complete the drop. For instance, a PictureBox might verify that the source control is another PictureBox and copy its picture.

Program Drag demonstrates drag and drop. It allows you to drag pictures from a control array of picDragFrom PictureBoxes into a control array of picDragTo PictureBoxes.

When you drag over the form or one of the picDragFrom pictures, the corresponding DragOver event handler sets the source control's DragIcon property to a "no drop" cursor. That makes it obvious to the user that a drop is not allowed. Visual Basic automatically displays a "no drop" cursor when you drag off of the application.

When you drag over a picDragTo control, the control verifies that the drag source control has name picDragFrom. If it does, the control changes the source control's DragIcon property to a "drag ok" cursor so it is obvious that a drop is allowed.

When you release the drag above a picDragTo control, the control's DragDrop event handler copies the source control's picture. Because the form and picDragFrom controls do not have DragDrop event handlers, they ignore drop events.

The following code shows how program Drag works.

Option Explicit

' When we enter the form, show the no drop cursor. 
Private Sub Form_DragOver(Source As Control, _
    X As Single, Y As Single, State As Integer)

    If State = vbEnter Then
        Source.DragIcon = picNoDrop.Picture
    End If
End Sub

' When we enter a picDragForm control, display
' the no drop cursor.
Private Sub picDragFrom_DragOver(Index As Integer, Source As Control, _
    X As Single, Y As Single, State As Integer)
    If State = vbEnter Then
        Source.DragIcon = picNoDrop.Picture
    End If
End Sub

' When we drag over a picDragTo control, display
' the ok drop cursor if the source control has
' name picDragFrom. Otherwise display the no
' drop cursor.
Private Sub picDragTo_DragOver(Index As Integer, Source As Control, _
    X As Single, Y As Single, State As Integer)

    ' If we are entering the control, see if
    ' we know how to accept the drop. 
    If State = vbEnter Then
        ' Verify that Source is a picDragFrom control. 
        If Source.Name = "picDragFrom" Then
            ' This drop is ok. 
            Source.DragIcon = picOkDrop.Picture
            Source.DragIcon = picNoDrop.Picture
        End If
    End If
End Sub

' Get the dropped picture if the source is
' named picDragFrom.
Private Sub picDragTo_DragDrop(Index As Integer, Source As Control, _
    X As Single, Y As Single)

    ' Make sure Source is as picDragFrom control. 
    If Source.Name <> "picDragFrom" Then Exit Sub

    ' Copy the dropped picture. 
    picDragTo(Index).Picture = Source.Picture
End Sub

Your Second Drag

Using the techniques described in Part I, you can add drag start and drag within a program. Now that you have these skills be sure to check out Part Two, to see how you can drag between programs in your Visual Basic applications!

Rod Stephens is a former senior member of GTE's technical staff. A programmer for over 13 years, Rod has designed and implemented more than a half dozen award-winning commercial applications, and author over 80 articles and seven books on Visual Basic Programming. Be sure to visit his VB education site, VB-Helper, at www.vb-helper.com!


Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.


We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date