Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / Visual Basic
Article

Example of Drag and Drop using Files

Rate me:
Please Sign up or sign in to vote.
4.90/5 (16 votes)
13 Nov 2008CPOL1 min read 59.9K   2.8K   23   9
I wrote a little example app that merges text files that you drag and dropped into the listview
Image 1

Introduction

Well this is just a pretty straightforward example of how to create a drag and drop feature that can list the file paths of the files that were dragged into the listview. Feel free to use it however you like... I hope this example helps some of the VB.NET developers.

Using the Code

There are really only two things that are needed to create a drag and drop feature...

First: You need to have a Private Sub Method that is: "DragEnter" and choose what kind of DataFormat you wish to use... In this example, I chose FileDrop.  You also need to choose what kind of effect you want the drag and drop feature to do. For instance, I chose "copy". You can choose whatever best fits with the type of application that you are designing.

Second: You will need to have a Private Sub Method that is: "DragDrop". This is where you want to specify what you want to do with the DataFormat that was copied. Below are examples of the first and second subs that I used to create the drag and drop feature. 

VB.NET
Private Sub lstFiles_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFiles.DragEnter
    'SET THE PROPER ACTION FOR FILE DROP....BY USING THE FILEDROP METHOD TO COPY
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    Else
        e.Effect = DragDropEffects.None
    End If
End Sub
VB.NET
Private Sub lstFiles_DragDrop(ByVal sender As Object, _
 ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFiles.DragDrop
     'NOW THIS IS WHAT SHOULD TAKE PLACE FOR THE APP TO ADD THE TEXT FILE
     ' PATHS TO THE LIST VIEW ITEMS
     If e.Data.GetDataPresent(DataFormats.FileDrop) Then
         Dim filePaths As String() = CType(e.Data.GetData_
             (DataFormats.FileDrop), String())
         For Each filePath As String In filePaths

             Dim chkExt As String = _
         My.Computer.FileSystem.GetFileInfo(filePath).Extension
             'I'M STILL LEARNING SO PLEASE DON'T CRITISIZE ME TO BAD....
             'THIS IS THE ONLY METHOD THAT I KNOW HOW TO CALCULATE THE FILE SIZE....
             Dim chkSize As String = _
         My.Computer.FileSystem.GetFileInfo(filePath).Length
             chkSize = chkSize / 1024

             'THIS IS WHAT I USED TO ROUND THE SIZE TO 2 DECIMAL POINTS...
             Dim d As Decimal
             d = Decimal.Round(chkSize, 2, MidpointRounding.AwayFromZero)

             'THIS WILL ADD THE FILEPATHS AND THE FILES SIZE TO THE LIST VIEW
             Dim LI As ListViewItem
             LI = Me.lstFiles.Items.Add(filePath)
             LI.StateImageIndex = 0
             LI.SubItems.Add(d & " kb")

         Next filePath
     End If
 End Sub

Points of Interest

Pretty simple really. I was just bored at the office and wanted something to do.

History

  • 13th November, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionadd a try catch in case the user selects a directory Pin
technobab22-Oct-21 5:03
technobab22-Oct-21 5:03 
Praisethank you Pin
Ivan Ferrer11-May-21 0:59
Ivan Ferrer11-May-21 0:59 
QuestionWPF Pin
Benoît Souletis20-Jun-19 23:55
Benoît Souletis20-Jun-19 23:55 
QuestionDrag Enter Pin
AlexF1852-Feb-18 1:23
AlexF1852-Feb-18 1:23 
BugDoes not work for me. Pin
shriram karpur22-Aug-14 8:26
shriram karpur22-Aug-14 8:26 
GeneralRe: Does not work for me. Pin
Rich In Soquel23-May-16 12:09
Rich In Soquel23-May-16 12:09 
GeneralMy vote of 4 Pin
D. Joe26-Apr-13 5:30
D. Joe26-Apr-13 5:30 
GeneralCategorize it Pin
#realJSOP13-Nov-08 5:39
mve#realJSOP13-Nov-08 5:39 
AnswerRe: Categorize it Pin
Rezied13-Nov-08 6:56
Rezied13-Nov-08 6:56 
Thanks for noticing that. This is my first article that I have ever posted to this site.... I will make the changes...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.