JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Determining What Items Are Checked in Checked List Boxes

You can determine if an item displays a checkmark in a checked list box using the GetItemChecked method, which returns True if an item is checked. For example, in the CheckedListBox example, I can loop over all items in the checked list box and display those that are checked in a text box, like this:

Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
    Dim intLoopIndex As Integer
    Dim strText, strData As String
    strText = "Checked Items: "

    For intLoopIndex = 0 To (CheckedListBox1.Items.Count - 1)
        If CheckedListBox1.GetItemChecked(intLoopIndex) = True Then
            strText &= CheckedListBox1.Items(intLoopIndex).ToString & ", "
        End If

    TextBox1.Text = strText
End Sub

You can see the results in Figure 7.10, where the text box is displaying the checked items.

Figure 7.10: Determining what items are checked in a checked list box.

That was the hard way; there's an easier way—you can just loop through the collection returned by the CheckedItems property, which holds all the checked items:

Dim strText, strData As String

strText = "Checked Items: "

For Each strData In CheckedListBox1.CheckedItems
    strText &= strData & ", "

TextBox1.Text = strText

Besides CheckedItems, there's also a CheckedIndices property, which returns a collection holding the indices of the checked items in the checked list box.

You also can use three-state checkboxes in checked list boxes, as discussed in the In Depth section of this chapter. In this case, you can use GetItemCheckState to determine the check state of an item. This method returns one of these values from the CheckState enumeration:

  • Checked— The control is checked.

  • Indeterminate— The control is indeterminate. An indeterminate control generally has a shaded appearance.

  • Unchecked— The control is unchecked.

Here's an example:

Dim State As CheckState
State = CheckedListBox1.GetItemCheckState(0)

Related solution:

Found on page:

Creating Three-State Checkboxes


Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor