Link to home
Start Free TrialLog in
Avatar of MISKid08
MISKid08

asked on

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
sb.append("'")
sb.append("1")
sb.append("'")

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.

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia 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
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
            sb.append("'")
            Select Case CheckedListBox1.CheckedItems(i).ToString
                Case "Red"
                    sb.Append("1")
                Case "Yellow"
                    sb.Append("2")
                    'and so on
            End Select
            sb.Append("'")
        Next

Avatar of MISKid08
MISKid08

ASKER

You're awesome. Thanks!