The short version of my rant at the end of the post is that
CheckedListBox is a complicated control and generally isn't worth it. You've already missed some details of how it works if you think the "Selected" properties and methods have anything to do with the check state, so let's discuss the control from a high level then look at some code examples.
Like a regular
ListBox,
CheckedListBox provides
Items,
SelectedIndex,
SelectedItem,
SelectedIndices, and
SelectedItems properties. Here's what each one does:
- Items gives you access to the value of all items, whether they are checked or selected.
- SelectedIndex gives you the index of the currently selected item, whether it is checked or not.
- SelectedIndices gives you a collection that contains the index of every selected item, whether it is checked or not.
- SelectedItem gives you the currently selected item's value, whether it is checked or not.
- SelectedItems gives you a collection that contains every selected item's value, whether it is checked or not.
- GetSelected() tells you whether the item with the specified index is selected, whether it is checked or not.
- SetSelected() lets you set whether an item with a specific index is selected or not.
The above are all you have to fool with if you're using a
ListBox. In addition to these properties, here's the
CheckedListBox-specific methods and properties:
- CheckedIndices gives you a collection that contains the index to every checked item, whether it is selected or not.
- CheckedItems gives you a collection that contains every checked item's value, whether it is selected or not.
- GetItemChecked() returns a Boolean that indicates whether an item is checked.
- GetItemCheckState() tells you whether an item is checked, unchecked, or invariant.
- SetItemChecked() lets you check or uncheck an item.
- SetItemCheckState() lets you make an item checked, unchecked, or invariant.
In a checked list box, an item can be in any one of 6 states based on whether it is selected, not selected, checked, unchecked, invariant, or any logical combination of selection and check state. You get items the same way you'd get items out of a
ListBox: the
Items property. You can use the
FindString() or
FindStringExact() methods to get the index of the item.
FindString() looks for values that start with the string you pass,
FindStringExact() looks for an exact match. Once you get the index, you have to use the various Get/Set functions to determine if the item is selected or checked.
Text is boring and examples speak louder, here's some code snippets that answer questions. All examples assume you're using strings as the values in the control:
"How do I get the value of the first selected item?"
Code:
If lstControl.SelectedIndices > 0 Then
value = lstControl.SelectedIndices(0).ToString()
Else
' There are no selected values
End If
"How do I get the value of all checked items?"
Code:
value = lstControl.CheckedItems.ToArray() ' ToArray() is specific to VS 2008
"How do I get the value of the first item that is both checked and selected?"
Code:
' (Tougher than you might think, there's no guarantee there is such an item and
' trying to use the CheckedIndices and SelectedIndices collection means you need to
' maintain two indexes and perform a somewhat complicated loop. Easier to use the brute
' force method)
Dim value As String = Nothing
For i As Integer = 0 To lstControl.Items.Length - 1
If lstControl.GetSelected(i) AndAlso lstControl.GetItemChecked(i) Then
value = lstControl.Items(i)
Exit For
End If
Next
' If value is Nothing, there is no item that is both checked and selected.
Unrelated statement: I have no idea why CheckedListBox is so popular. It's hard to understand how to use it for both the programmer and the user. For most use cases, a multi-select ListBox behaves exactly the same way and is more familiar to the user. Quick quiz: how many CheckedListBox controls can you think of that are used in a Microsoft or other major software developer's program? I can think of 0. Lack of use usually indicates a reason, and in this case I'd say the reason is the control introduces complexity without adding much value.