Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drag and Drop to Listview [Tutorial]
#1
Here's a little sample code of my own that I put together using the different strings in an array of the split function to add dropped files with a file extension to a listview in multiple columns:

[Image: ibe52XrjPG0XDo.png]

Code:
Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    Private Sub ListView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
        Dim file As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop, True), System.String())
        For Each s As String In file
            Dim parts() As String = s.Split(CChar("\"))

            Dim n As Integer = -1
            For Each Str As String In parts
                n += 1
            Next

            If parts(n).Contains(".") Then
                Dim Filename As String = parts(n).Substring(0, parts(n).IndexOf("."))
                Dim Location As String = s.Replace(parts(n), String.Empty)
                Dim Filetype As String = (parts(n).Substring(parts(n).LastIndexOf("."), parts(n).Length - parts(n).LastIndexOf("."))).Replace(".", String.Empty)

                Dim ItemAdd() As String = {Filename, Location, Filetype}

                Dim ListVItem = New ListViewItem(ItemAdd)
                ListView1.Items.Add(ListVItem)
            End If

            Exit Sub
        Next
    End Sub

In your form load event handler you'll want to do something like this:
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListView1.AllowDrop = True

        'Setting the column widths
        ListView1.Columns.Item(0).Width = 175
        ListView1.Columns.Item(1).Width = 275
        ListView1.Columns.Item(2).Width = 75
    End Sub

This is just to set the allow drop property to true so that you can drop files to the control, and the width's of the different columns you can change to your own desire.

Our Parts() array is defined by each in between value separated by the "\" new directory identifier in the filesystem.

Code:
Dim n As Integer = -1
            For Each Str As String In parts
                n += 1
            Next

What we are doing here is counting the number of array items so that we are able to get the last index of the parts() array to deal with as our filename, without the filepath. (Later on we take this last index of the string array and cut out the "." to define our substring for the file extension in the last column)

Code:
If parts(n).Contains(".") Then
                Dim Filename As String = parts(n).Substring(0, parts(n).IndexOf("."))
                Dim Location As String = s.Replace(parts(n), String.Empty)
                Dim Filetype As String = (parts(n).Substring(parts(n).LastIndexOf("."), parts(n).Length - parts(n).LastIndexOf("."))).Replace(".", String.Empty)

                Dim ItemAdd() As String = {Filename, Location, Filetype}

                Dim ListVItem = New ListViewItem(ItemAdd)
                ListView1.Items.Add(ListVItem)
            End If

You want this:
Code:
If parts(n).Contains(".") Then

Because we dont' want to be adding directories. If you need to use a file that doesn't have a file extension i'm sure you can add in a bit of code of your own to define some unique file extension like ".myfile" since that's not going to change the properties of the file itself. Changing the file extension doesn't do that.

throughout this bit of code, parts(n) is now our placeholder for our last index of the string array for the split function. n holds a different value/number depending on the number of directories the file is embedded in. The depth of the folder inside a given number of folders.

After defining all of our parts, we use this to add them to the row:
Code:
Dim ItemAdd() As String = {Filename, Location, Filetype}

                Dim ListVItem = New ListViewItem(ItemAdd)
                ListView1.Items.Add(ListVItem)
            End If

This just adds the collection of items

You can modify the code yourself by adding a loop if you want to add more than one file at a time.
Reply
#2
This is complicated for beginners, but deffinately a good, helpful thread! Looks nice and clean. This also can be used in many different programs.
[Image: iddyEs.png]
Reply
#3
That could be why i've had no replies with it yet. I did my best to explain it, but this is my code snippet that i've created, and so far no bugs. It doesn't check folders because they don't hold a file extension, but it also doesn't check files without file extensions.
Reply
#4
Thank you, this will be useful in a program I'm working on.
Can't spare the details howeverSmile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,515 08-30-2012, 10:14 PM
Last Post: Resistance
  [NEED HELP] how to read a text file and import to listview vb.net w00pz 7 6,385 02-13-2012, 06:42 AM
Last Post: AceInfinity
  ListView Custom Search Filter - List(Of T) Method AceInfinity 4 3,699 12-31-2011, 03:35 PM
Last Post: AceInfinity
  {TuToRiAl} How To Verify If Your App. Was Ran As Administrator {TuToRiAl} Resistance 11 6,013 10-11-2011, 04:30 PM
Last Post: Genuine
  [VB.NET] - ListBox Drag & Drop - [TUT] Fragma 10 7,718 08-25-2011, 07:02 PM
Last Post: AceInfinity

Forum Jump:


Users browsing this thread: 2 Guest(s)