Home > Software design >  Winforms. How to grab Checkbox values from form?
Winforms. How to grab Checkbox values from form?

Time:07-15

I have an winform app. There are 5 checkbox's. I need grab their values into array.

Example:

CheckBox's 1-3 are disabled, 4-5 enabled.

In output I want to have an array like this - [False, False, False, True, True].

How can I do this? There's my code I'll tried before (I'll used a list for it, and, yeah, I know, I checked only Checked. But I dont find any method of CheckBox to check their False value):

List<bool> properties = new List<bool>();

foreach (CheckBox checkBox in this.Controls.OfType<CheckBox>())
{
    if (checkBox.Checked)
    {
        properties.Add(checkBox.Checked);
    }
}

And yeah, I will convert List to an Array after

CodePudding user response:

You have to delete your if condition so every foreach loop can check each checkbox's checked property and add it to your list. (I tried it and works fine)

List<bool> properties = new List<bool>();
foreach (CheckBox checkBox in this.Controls.OfType<CheckBox>())
  { 
     properties.Add(checkBox.Checked); 
  }
  • Related