在这里首先感谢网友Ice_Age
删除评论2008-11-3 22:13:56
foreach (Control c in Controls) { if (c is CheckBox) { ((CheckBox) c).Checked = pdwPortVal & (1 << c.TabIndex)) } } index 也可以放在 Tag 中。
删除评论2008-11-4 21:53:18
你可以把3组checkbox放入groupbox, panel 等container中,checkbox的索引值放入 tabindex或tag中,则程序很简洁。
//遍历CheckBox方法1
foreach (Control c in groupBox1.Controls)//遍历groupBox1内的所有控件
{
if (c is CheckBox)//只遍历CheckBox控件
{
((CheckBox)c).Checked = (pdwPortVal & (1 << (checkBox8.TabIndex - c.TabIndex))) != 0;
}
}
//遍历CheckBox方法2
/*
for (int i = 8; i >= 1; i--)
{
((CheckBox)this.Controls.Find("checkBox" + i, true)[0]).Checked = (pdwPortVal & (1 << (8 - i))) != 0;
}
*/
/*
//遍历CheckBox方法3
for (int i = 0; i < 8; i++)
{
switch (i)
{
case 0:
checkBox8.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 1:
checkBox7.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 2:
checkBox6.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 3:
checkBox5.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 4:
checkBox4.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 5:
checkBox3.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 6:
checkBox2.Checked = (pdwPortVal & (1 << i)) != 0;
break;
case 7:
checkBox1.Checked = (pdwPortVal & (1 << i)) != 0;
break;
}
}
*/
用户222685 2009-5-5 13:57