Reputation: 4286
I have checkboxlist in my windows form application and I would like to check if at least one checkbox is checked. If so rest of my code will execute ,if not error message should be displayed.How can I do that?
Upvotes: 2
Views: 4107
Reputation: 9857
You can do this
foreach(var cb in checkboxlist){
if(cb is CheckBox && cb.isChecked)
{
//Your Custom code here
}
}
Or you can point all of the Checkboxes at the same isChecked event. This is probably a cleaner option than the foreach but it really depends on how you need to put it in.
I will leave my answer up for comparison but the two that use CheckedIndices are way better solutions.
Upvotes: 0
Reputation: 26209
if (checkedListBox1.CheckedIndices.Count > 0)
{
//Selected some items
}
Upvotes: 6