C# Cleaner way to check for checkbox states?

18,366

Solution 1

Make an array of those checkboxes. You can make all that a for loop.

for(int i = 0; i < checklist.Length; i++)
{
    if (checklist[i].Checked == true)
    {
        arrayOfCheckboxes[i].Checked = true;
        arrayOfCheckboxes[i].CheckState = CheckState.Checked;
    }
}

Solution 2

You could change your List into a Dictionary and do a simple enumeration though them like so:

public PointCtrlRowSelectionForm(Dictionary<CheckBox, Boolean> checkList, PointCtrlForm form, string title)
        {
            InitializeComponent();
            this.form = form;
            this.Text = title;

            foreach<KeyValuePair<CheckBox, Boolean> kvp in checklist)
            {
                if (kvp.Value == true)      
                {
                    kvp.Key.Checked = true;
                    kvp.Key.CheckState = CheckState.Checked;
                }
            }
        }

Solution 3

Unfortunately there is no Control Grouping feature supported by the IDE form designer. Depending on how you design your form, there are 4 methods I can think of:

  1. Change the auto-generated code and add the CheckBox instances to an array (not recommended).
  2. Keep the auto-generated code intact but create a second array referring to each CheckBox.

    CheckBox[] checkboxes = new CheckBox[]{checkBox1, checkBox2, checkBox3};
    
  3. Put all your Checkbox instances into a group control. You can then cycle through the Controls property of this group. Use the Tag property of each CheckBox to select an index (an alternative to this could be to use the TabIndex instead or to rely on the order in which the CheckBox instances are found).

    bool[] check_list = new bool[] { true, true, false };
    private void button3_Click(object sender, EventArgs e)
    {
      foreach (Control c in groupBox1.Controls)
      {
        if (c is CheckBox)
        {
          CheckBox current = c as CheckBox;
          current.Checked = check_list[int.Parse(current.Tag.ToString())];
        }
      }
    }
    
  4. Do the same as the previous solution but on your main form.

Share:
18,366
TtT23
Author by

TtT23

Updated on June 30, 2022

Comments

  • TtT23
    TtT23 almost 2 years

    I have the following code which takes List of boolean as a parameter then sets the checked state of each checkList by individually verifying the list.

    Is there a much more efficient way of writing the following code? For instance, by using a loop?

    public PointCtrlRowSelectionForm(List<Boolean> checkList, PointCtrlForm form, string title)
                {
                    InitializeComponent();
                    this.form = form;
                    this.Text = title;
                    if (checkList[0] == true)      
                    {
                        checkBox1.Checked = true;
                        checkBox1.CheckState = CheckState.Checked;
                    }
                    if (checkList[1] == true)
                    {
                        checkBox2.Checked = true;
                        checkBox3.CheckState = CheckState.Checked;
                    }
                    if (checkList[2] == true)
                    {
                        checkBox3.Checked = true;
                        checkBox3.CheckState = CheckState.Checked;
                    }
                    if (checkList[3] == true)
                    {
                        checkBox4.Checked = true;
                        checkBox4.CheckState = CheckState.Checked;
                    }
                    if (checkList[4] == true)
                    {
                        checkBox5.Checked = true;
                        checkBox5.CheckState = CheckState.Checked;
                    }
                    if (checkList[5] == true)
                    {
                        checkBox6.Checked = true;
                        checkBox6.CheckState = CheckState.Checked;
                    }           
                }
    
  • Samy Arous
    Samy Arous almost 12 years
    This solution might cause some issues when it comes to multithreading and serialization + the checkboxes might not be available when the checklist values are fetched + At some point, you will still have to feed data into your dictionary and I can't see how you would do that without passing each checkbox independently. I might be wrong though