Search code examples
c#checklistbox

Determining Whether a Selected CheckListBox Item is Checked


There is so little information out there about the CheckListBox that I'm wondering if people aren't using something else instead.

I am trying to use a conditional statement on a MouseUp event to determine if the selected check list box item is checked or unchecked. The following code does not work:

if (clBox.SelectedItem == CheckState.Checked)
{
   //Do something
}

How can I determine whether or not the selected CheckListBox item is checked? I have to use the MouseUP event because using the ItemCheck event is troublesome when some boxes may be checked when added to the list. Otherwise I end up triggering the event. Yet how do I make sure something is undone when they uncheck the box vs. done when they check it?

EDIT: Forgot to mention that this is Windows Forms.


Solution

  • You can check the CheckedItems collection to find if the SelectedItem is contained in it. Try something like this.

    private void clBox_MouseUp(object sender, MouseEventArgs e)
    {
        if (clBox.CheckedItems.Contains(clBox.SelectedItem))
        {
            MessageBox.Show("Test");
        }
    }