Display the number of items checked in a CheckedListBox in C#

The CheckedListBox control has a CheckedItems collection that holds references to the control's currently checked items. You can use the collection's Count property to determine the number of items checked.

When the user checks or unchecks an item, the control raises its ItemCheck event to let you know. Unfortunately the control's CheckedItems collection has not yet been updated so the collection's Count does not give you the value it will have after the item is checked or unchecked.

To display an accurate count, you need to look at the event handler's e.CurrentValue and e.NewValue parameters to determine whether the item should be included in the count.

When you check and uncheck items in this example, the following ItemCheck event handler displays the total number if items in the CheckedListBox and the number of items that are selected.

// Update the display of files checked.
private void clbFiles_ItemCheck(object sender, ItemCheckEventArgs e)
    // Get the current number checked.
    int num_checked = clbFiles.CheckedItems.Count;

    // See if the item is being checked or unchecked.
    if ((e.CurrentValue != CheckState.Checked) &&
        (e.NewValue == CheckState.Checked))
    if ((e.CurrentValue == CheckState.Checked) &&
        (e.NewValue != CheckState.Checked))

    // Display the count.
    lblCount.Text = clbFiles.Items.Count + " items, " +
        num_checked + " selected";

The code gets the current number of items checked. Then depending on the e.CurrentValue and e.NewValue parameters, it updates that count. The event handler finishes by displaying the total number of items and the number of checked items.

This example demonstrates one other useful technique for using CheckedListBoxes. Normally you must select an item in the list to give it focus and then click its box to check it. That means you need to click the item twice to check or uncheck it.

You can make this easier and less annoying if you set the control's CheckOnClick propertry to true. Then when the user clicks an item in the list, it is checked or unchecked.

This example's Form_Load event handler uses the following statement to set this property (or you can do it at design time).

clbFiles.CheckOnClick = true;



What did you think of this article?

  • No trackbacks exist for this post.
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.


 Email (will not be published)


Your comment is 0 characters limited to 3000 characters.