Loop to check values that are checked in a CheckedListBox

I don't even know if this is possible, but I am having problems changing a checked CheckedListBox value to something else.

For example, I have a CheckedListBox with 3 items. We will say the colors Red, Blue, and Green. However, in an outside database the same colors are represented by the numbers 1, 2, and 3. So, once the user selects one or multiple colors from the checkedlistbox, I then need to append/concat a string with the colors converted to their appropriate numbers.

For instance, if the user selected "Red" and "Green" I would need to return a string that says "'1,3'".

Dim sb as New StringBuilder
If clb.CheckedItems.ToString = "Red" Then

This returns nothing. For testing purposes, I am just outputting it in a msgbox.

If clb.SelectedItems.ToString = "Red" Then... works, but also works if the item is selected/highlighted. I need only checked items to be converted.

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.

Wayne Taylor (webtubbs)Commented:
You can use the below code to get the concatenated string containg your color numbers.

        Dim s As String = ""
        For Each i As Integer In Me.CheckedListBox1.CheckedIndices
            Select Case Me.CheckedListBox1.Items(i).ToString
                Case "Red" : s &= "1,"
                Case "Blue" : s &= "2,"
                Case "Green" : s &= "3,"
            End Select
        s = s.Substring(0, s.Length - 1) 'remove the trailing comma 

Open in new window


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
Hi again,

You need to loop through the array of CheckedItems, similar to the example I gave you before:

 Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
            Select Case CheckedListBox1.CheckedItems(i).ToString
                Case "Red"
                Case "Yellow"
                    'and so on
            End Select

MISKid08Author Commented:
You're awesome. Thanks!
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.