Loop Through FileInfo objects in a ComboBox

Posted on 2009-12-24
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
    LVL 11

    Accepted Solution

    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

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

    Expert Comment

    by:Jeff Certain
    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.
    LVL 1

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    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…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now