C# Cleaner way to check for checkbox states?
.net, c#, checkbox, winforms
Solution
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;
}
}
Problem
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; } } ```