我有以下的code这需要布尔值的列表作为参数,然后通过单独验证列表中设置每个检查表的选中状态。
有没有写以下code的一个更有效的方法是什么?例如,通过使用循环?
公共PointCtrlRowSelectionForm(名单<布尔>的清单,PointCtrlForm形式,串题)
{
的InitializeComponent();
this.form =形式;
this.Text =称号;
如果(检查表[0] ==真)
{
checkBox1.Checked = TRUE;
checkBox1.CheckState = CheckState.Checked;
}
如果(检查表[1] ==真)
{
checkBox2.Checked = TRUE;
checkBox3.CheckState = CheckState.Checked;
}
如果(检查表[2] ==真)
{
checkBox3.Checked = TRUE;
checkBox3.CheckState = CheckState.Checked;
}
如果(检查表[3] ==真)
{
checkBox4.Checked = TRUE;
checkBox4.CheckState = CheckState.Checked;
}
如果(检查表[4] ==真)
{
checkBox5.Checked = TRUE;
checkBox5.CheckState = CheckState.Checked;
}
如果(检查表[5] ==真)
{
checkBox6.Checked = TRUE;
checkBox6.CheckState = CheckState.Checked;
}
}
解决方案
请这些复选框的数组。你可以让所有的for循环。
的for(int i = 0; I< checklist.Length;我++)
{
如果(检查表[I] .Checked ==真)
{
arrayOfCheckboxes [I] .Checked = TRUE;
arrayOfCheckboxes [I] .CheckState = CheckState.Checked;
}
}
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;
}
}
解决方案
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;
}
}