Loop to check values that are checked in a CheckedListBox

Posted on 2008-11-17
Last Modified: 2012-05-05
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.

Question by:MISKid08
    LVL 46

    Accepted Solution

    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

    LVL 20

    Expert Comment

    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


    Author Closing Comment

    You're awesome. Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    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…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now