10

Is it possible to apply .Checked== to checkedlistbox as in checkbox?

If to do it in a way as with checkbox it not works

if(checkedListBox1.Items[2].Checked==true)
{
}

10 Answers 10

12

What you need is the method GetItemCheckState.

Usage as follows:

if(checkedListBox1.GetItemCheckState(2) == CheckState.Checked)
{

}
7

You can use it in this way

if (checkedListBox1.CheckedItems.Contains("ItemWithIndex2"))
{
    MessageBox.Show("Test");
}
1
  • 1
    this could result in wrong behaviour if there are several items with the same name
    – Breeze
    Commented Apr 15, 2016 at 8:12
3

Try something like...

checkedListBox1.GetItemChecked(i)

foreach(int indexChecked in checkedListBox1.CheckedIndices) {
    // The indexChecked variable contains the index of the item.
    MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +
                    checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
}
1

GetItemChecked() returns a boolean value. So you can use it as the following:

if(checkedListBox1.GetItemChecked(index) == true) {

}

Where index is an integer value denoting the row index of checkedListBox1.

0
0

GetItemCheckState() returns a boolean value. So you can use as follows:

if(checkedListBox1.GetItemCheckState(index) == true)
{

}

where index is an integer value denoting the row index of CheckedListBox

0

you might be looking for something like this

foreach(int i in checkedListBox1.SelectedIndices)
        {
            if(checkedListBox1.GetItemCheckState(i)!=CheckState.Checked)
            {
                ....
            }
        }
0

I ran into a similar issue: On a click of an item, the state should be converted from either checked/ non-checked to opposite. Here i post the event and the check and change:

    CheckedListBox ChkLBox;
    private void CheckedListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        int SelectedIndex = ChkLBox.SelectedIndex; //
        var Item = ChkLBox.Items[SelectedIndex];

        bool IsChecked = (ChkLBox.GetItemChecked(ChkLBox.SelectedIndex));
        ChkLBox.SetItemChecked(ChkLBox.Items.IndexOf(Item), !IsChecked);
    }
-1

checkedListBox1.CheckedItems.Count>0

-1
var itemChecked = checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex);
1
  • 5
    Welcome to stackoverflow. Code-only answers are not ideal, you should consider adding some explanations.
    – XouDo
    Commented Jun 8, 2021 at 7:41
-2

I'm not sure i understand your question, do you want to check if at least 1 item in the listbox is checked? If so you could do that

if(checkedListBox1.Items.Any(item=>item.Checked))
{
}
1
  • Items contains strings, so item.Checked won't work
    – Breeze
    Commented Apr 15, 2016 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.