I was working on a method and then I realized that I had a foreach loop that ran through all checkedItems, instead of running through all unchecked item.
foreach ( object itemChecked in checkedListBox1.CheckedItems)
{(...)}
I was wondering if there is way to do this without changing the code too much. Regards
Two options:
Items
and check them against the CheckedItems
.Option 1
foreach (object item in checkedListBox1.Items)
{
if (!checkedListBox1.CheckedItems.Contains(item))
{
// your code
}
}
Option 2
IEnumerable<object> notChecked = (from object item in checkedListBox1.Items
where !checkedListBox1.CheckedItems.Contains(item)
select item);
foreach (object item in notChecked)
{
// your code
}
Cast the items as a CheckBox enumerable then you can loop:
foreach (CheckBox cb in checkedListBox1.Items.Cast<CheckBox>())
{
if (!cb.Checked)
{
// your logic
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With