Count and access checkbox marked

Asked

Viewed 504 times

1

I need to count checkboxes that are marked on a panel and are horizontal. It’s Windows form, C#. I couldn’t format a listbox to the horizontal, so it’s in a panel. But I’d like to count them and access them with a loop. I tried to concatenate the names that are in sequence, tried via code, also without success. Anyone has any suggestions?

1 answer

3


The fact of Chckbox’s be in a Dashboard will facilitate your counting in a loop.
The object Dashboard keeps a list of objects of type Control that are on the "inside" of the property Controls.
Just go through this list and when you find a Checkbox, check if you are checked and count them.

private int getCheckedCount()
{
    int count = 0;
    foreach (Control control in panel1.Controls)
    {
        if (control is CheckBox) 
        {
            if(control.Checked) count++;
        }
    }
    return count;
}  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.