简体   繁体   中英

C# Checkedlistbox if checked

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)
{
}

What you need is the method GetItemCheckState .

Usage as follows:

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

}

You can use it in this way

if (checkedListBox1.CheckedItems.Contains("ItemWithIndex2"))
{
    MessageBox.Show("Test");
}

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() + ".");
}

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

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 .

you might be looking for something like this

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

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);
    }

checkedListBox1.CheckedItems.Count>0

var itemChecked = checkedListBox1.GetItemChecked(checkedListBox1.SelectedIndex);

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))
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM