Amila Iddamalgoda
Amila Iddamalgoda

Reputation: 4286

Check if at least one checkboxlist is selected in windows form

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

Answers (3)

DotNetRussell
DotNetRussell

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.

Edit

I will leave my answer up for comparison but the two that use CheckedIndices are way better solutions.

Upvotes: 0

w.b
w.b

Reputation: 11228

if (checkedListBox.CheckedItems.Count > 0)
{
}  

Upvotes: 1

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

      if (checkedListBox1.CheckedIndices.Count > 0)
       {

        //Selected some items
       }

Upvotes: 6

Related Questions