Loop Through FileInfo objects in a ComboBox

I am adding files to a ComboBox like so:

For Each fReports As FileInfo In dirReports.GetFiles
        tscboxReports.Items.Add(fReports)
Next

I need to be able to list the contents of the appropriate file when the user selects an item. The listing part I already have figured out. What I need to know is how to loop through the items and then commit then action on that file.

I tried this:

For Each item As FileInfo In tscboxReports.SelectedItem
'Do Work
Next

I also tried it as an Object, but it still did not work.

This is the error I've received:

Unable to cast object of type 'System.IO.FileInfo' to type 'System.Collections.IEnumerable'.

The error is the same no matter if I loop through them as 'Objects' or 'FileInfos'.

Thanks
LVL 1
weirddemonAsked:
Who is Participating?
 
PlatoConsultantConnect With a Mentor Commented:
u r missing the casting operation '

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("c:\")
        ' Get a reference to each file in that directory.
        Dim fiArr As FileInfo() = di.GetFiles()
        ' Display the names of the files.
        Dim fri As FileInfo
        For Each fri In fiArr

            Me.ComboBox1.Items.Add(fri)

        Next fri
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fiArr As FileInfo = Me.ComboBox1.SelectedItem
        MsgBox(fiArr.Length)

    End Sub
End Class

Open in new window

0
 
PlatoConsultantCommented:
ComboBox1.SelectedItem is not a collection of object hence can not be used  in ( for  in loop)
 
0
 
Jeff CertainCommented:
Selected item in a combo box will only have a single item. You'll either need to enumerate across the Items collection, or use a multi-select ListBox.
0
 
weirddemonAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.