easy CheckedListBox question using vb .net 2005



Okay guys this is pretty easy, yet I simply am not sure what to search to find the answer. I have a checked list box. I want it to work kind of like a drop down list where you can have a text field that is displayed and a value field that is the actual value used.

In my checked list box, I am adding a list of files in a given directory. I created a sloppy class to test the above and I'm not getting the results expected.


        files = Directory.GetFiles(fldrdlgTargetFolder.SelectedPath)

        For Each strFile In files
            chk.fullfile = strFile
            chk.shortfile = strFile.Substring(strFile.LastIndexOf("\"))
            chklbTargetFiles.Items.Add(chk)
            chk = New chunk
        Next

All of this is in the Form1 class. So is the following definiton for the class chunk:

   Private Class chunk
        Public fullfile As String
        Public shortfile As String
    End Class

Like I said, this was just meant to be quick and dirty. I am trying to display the shortfile attribute of chunk in the checkedlistbox. When I get the items that are selected, I want to the attribute fullfile from the chunk object stored for the given item(s).

When the program runs the text displayed for each item added is: FileCopier.Form1 + chunk

Am I missing something small or just barking up the wrong tree with this?

jbaisdenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jbaisdenAuthor Commented:


This is a vb .net Windows application in VS 2005 by the way.
jbaisdenAuthor Commented:


What would also help is a good reference point for how to use the CheckedListBox class. I've primarily done Web programming, so I'm just getting into the windows forms environment. I had expected the items stored in this control to work more like a datagriditem or something, but it seems really difficult to access a single item in this control. I'm trying to access an object at a given index in the object collection so I can adjust the checked property/attribute (If this event exists).

I'm just really confused and badly need a reference point that is more clear than the class documentation for the CheckedListBox class.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics TeacherCommented:
Try this out...

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim c As chunk
        Dim di As New System.IO.DirectoryInfo("c:\")
        Dim fi As System.IO.FileInfo
        For Each fi In di.GetFiles()
            c = New chunk(fi)
            CheckedListBox1.Items.Add(c)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim c As chunk
        For Each c In CheckedListBox1.CheckedItems
            Debug.WriteLine(c.ShortFile & " --> " & c.FullFile)
        Next
    End Sub

    Private Class chunk

        Private Sub New()
        End Sub

        Public Sub New(ByVal fi As System.IO.FileInfo)
            Me.fi = fi
        End Sub

        Private fi As System.IO.FileInfo

        Public ReadOnly Property File() As System.IO.FileInfo
            Get
                Return fi
            End Get
        End Property

        Public ReadOnly Property FullFile() As String
            Get
                Return fi.FullName
            End Get
        End Property

        Public ReadOnly Property ShortFile() As String
            Get
                Return fi.Name
            End Get
        End Property

        Public Overrides Function ToString() As String
            Return Me.ShortFile
        End Function

    End Class

End Class

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jbaisdenAuthor Commented:

Yep that did it. I was just missing the:

       Public Overrides Function ToString() As String
            Return Me.ShortFile
        End Function

Once I added that, everything was fine.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.