Link to home
Start Free TrialLog in
Avatar of weirddemon
weirddemon

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of PlatoConsultant
PlatoConsultant
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ComboBox1.SelectedItem is not a collection of object hence can not be used  in ( for  in loop)
 
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.
Avatar of weirddemon
weirddemon

ASKER

Thanks