[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Loop Through FileInfo objects in a ComboBox

Posted on 2009-12-24
Medium Priority
Last Modified: 2013-12-26
I am adding files to a ComboBox like so:

For Each fReports As FileInfo In dirReports.GetFiles

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

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'.

Question by:weirddemon
  • 2
LVL 11

Accepted Solution

PlatoConsultant earned 2000 total points
ID: 26121481
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


        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

    End Sub
End Class

Open in new window

LVL 11

Expert Comment

ID: 26121491
ComboBox1.SelectedItem is not a collection of object hence can not be used  in ( for  in loop)
LVL 24

Expert Comment

by:Jeff Certain
ID: 26122108
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.

Author Closing Comment

ID: 31669885

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question