08-26-2011, 01:02 PM
(This post was last modified: 08-26-2011, 01:10 PM by AceInfinity.)
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:
In your form load event handler you'll want to do something like this:
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.
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)
You want this:
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:
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.
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.