Loop to check values that are checked in a CheckedListBox

Posted on 2008-11-17
Medium Priority
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 48

Accepted Solution

Wayne Taylor (webtubbs) earned 1000 total points
ID: 22981660
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

ID: 22981662
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

ID: 31517710
You're awesome. Thanks!

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

624 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