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 sub
s that I used to create the drag and drop feature.
Private Sub lstFiles_DragEnter(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFiles.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub lstFiles_DragDrop(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFiles.DragDrop
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
Dim chkSize As String = _
My.Computer.FileSystem.GetFileInfo(filePath).Length
chkSize = chkSize / 1024
Dim d As Decimal
d = Decimal.Round(chkSize, 2, MidpointRounding.AwayFromZero)
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
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.