C#清除检查复选框状态的方法?

l46*_*kok 3 .net c# checkbox winforms

我有以下代码,它将布尔值列表作为参数,然后通过单独验证列表来设置每个checkList的检查状态.

是否有更有效的方式编写以下代码?例如,通过使用循环?

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;
                }           
            }
Run Code Online (Sandbox Code Playgroud)

Ceh*_*iro 8

创建一个这些复选框的数组.你可以做所有的for循环.

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