Drag & Drop Files into Your Form or Control

To drop a file into your form and make your form accept it is quite easy. All we need to do is set the AllowDrop property of the control to True and then trap the appropriate Drag events.

For our first demo we will drop any file from windows explorer into the form, and let the form open that file for us.

For our second demo, we will add a listbox and picturebox to our form. Image files can be dragged and dropped into the listbox. And selecting any item in the listbox should show the image in the picturebox.

Demo 1

Start a new VB.NET forms application and add the following code to the form:

Private Sub Form1_Load(ByVal sender As System.Object, _
                      
ByVal e As System.EventArgs) _
                      
Handles MyBase.Load
   
” first allow the form to accept drop.
    Me.AllowDrop = True
End
Sub

 

Private Sub Form1_DragEnter(ByVal sender As Object, _
                           
ByVal e As System.Windows.Forms.DragEventArgs) _
                           
Handles Me.DragEnter
   
” when a file is dragged into our form,
    ” we change the mouse icon to signal the user that it can be dropped there.
    If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub

 

Private Sub Form1_DragDrop(ByVal sender As Object, _
                          
ByVal e As System.Windows.Forms.DragEventArgs) _
                          
Handles Me.DragDrop
   
” when the file is dropped on the form, we get the filename from it.
    Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
 
   
” lets open the first file to demonstrate that we know what was dropped.
    Process.Start(files(0))
End Sub

 

Run the application. Drag any file from your desktop or Windows Explorer on to this form. It will automatically open the file. If you drag more than one files, it will open the first file (for demonstration).

In the code above, I showed dropping files on to the form. In fact you can use any control on the form instead. All you need to do is set the control’s AllowDrop property to true and code the control’s Drag events as shown above.

Demo 2

For our second demo, we add a ListBox and a PictureBox to our form, and the following code:

Private Sub Form1_Load(ByVal sender As System.Object, _
                      
ByVal e As System.EventArgs) _
                      
Handles MyBase.Load
    ListBox1.AllowDrop =
True
End
Sub

 

Private Sub ListBox1_DragEnter(ByVal sender As Object, _
                              
ByVal e As System.Windows.Forms.DragEventArgs) _
                              
Handles ListBox1.DragEnter
    If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub

 

Private Sub ListBox1_DragDrop(ByVal sender As Object, _
                             
ByVal e As System.Windows.Forms.DragEventArgs) _
                             
Handles ListBox1.DragDrop
   
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
    ListBox1.DataSource = files

End Sub

 

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
                                         
ByVal e As System.EventArgs) _
                                         
Handles ListBox1.SelectedIndexChanged
    PictureBox1.Image = Image.FromFile(ListBox1.SelectedItem.ToString)
End Sub

 

Run the code. Drag some image files into the ListBox. Notice that they get added to the listbox. And selecting any one of them shows it in the PictureBox.

How the Code Works

In the Form Load event, we set the AllowDrop property of the control/form to True. This allows the control to monitor what is being dragged over it, and appropriately fire the DragEnter, DragLeave, DragOver, DragDrop events. The ‘e’ argument (of type DragEventArgs) contains information about the object being dragged. If we find that something of our interest is being dragged over our control, we take whatever action we want to in the appropriate events.

Of course, I haven’t put any validation code or error handlers to verify the filetype etc. in either of the demos, to keep the code simple and to the point. Ideally when developing applications, such checks are necessary to make your program run smoothly.

Update (March 05, 2010):

Further Readings:

Implementing Drag and Drop in Visual Basic.NET
This is a nice article worth reading. It also has a good explanation for people migrating from older versions of Visual Basic (VB6).
Advertisements

4 Responses to “Drag & Drop Files into Your Form or Control”

  1. Paul Says:

    Great tutorial, how would i drag a file into a file dialog open box?

  2. pradeep1210 Says:

    A file dialog box is different from your form. They are not the same thing. And whether it would support a file drag & drop depends on its implementation. Fortunately the file open dialog supports file drag & drop. So you should have no problems having to drag & drop files into it.

  3. Arijit Says:

    very helpful, how would i drag a picture from a picturebox into a listbox??

  4. viraj Says:

    Thanks a lot


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: