Forum Home
Press F1
 
Thread ID: 118326 2011-05-30 10:40:00 Files in a ListView bot (15449) Press F1
Post ID Timestamp Content User
1205646 2011-05-30 10:40:00 So, what I am trying to do is put a collection of files selected in an OpenFileDialog into a ListView column. I tried the code below and it did this:


Unable to cast object of type 'System.String[]' to type 'System.Windows.Forms.ListViewItem[]'.


Dim fileListOld As Array = OpenFileDialog1.FileNames()
ListView1.Items.AddRange(fileListOld)

So what's wrong?

VB 2010 BTW.
bot (15449)
1205647 2011-05-30 14:00:00 Hi, I'm not sure if you still need this but here is the code that I came up with real quick.



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()

Dim fileList As Array = OpenFileDialog1.FileNames()

For Each fileName As String In fileList

ListView1.Items.Add(fileName)

Next
End Sub


edit: whats wrong is that the addRange() method doesn't allow an array to passed as a variable. I think (from memory) that if you stored the file names in a List<> that you would be able to use the addRange() method.

Note: this is to the best of my knowledge (which is not particularly vast XD)
Fifthdawn (9467)
1205648 2011-05-31 08:36:00 Hi, I'm not sure if you still need this but here is the code that I came up with real quick.



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()

Dim fileList As Array = OpenFileDialog1.FileNames()

For Each fileName As String In fileList

ListView1.Items.Add(fileName)

Next
End Sub


edit: whats wrong is that the addRange() method doesn't allow an array to passed as a variable. I think (from memory) that if you stored the file names in a List<> that you would be able to use the addRange() method.

Note: this is to the best of my knowledge (which is not particularly vast XD)

That worked brilliantly, thanks a lot :D


Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim fileList As Array = OpenFileDialog1.FileNames()
Dim playlocation As String
For Each fileName As String In fileList
Dim lastSlash As Integer = OpenFileDialog1.FileName.LastIndexOf("\")
playlocation = fileName.Substring(lastSlash + 1)
ListView1.Items.Add(playlocation)
Next
End Sub


Here is the (polished) code for anyone doing a Google search :D
bot (15449)
1