本文来介绍C# 遍历CheckBox方法,笔者精心准备了三种方法供大家选择,程序很简单,很好懂的。
- //遍历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;
- }
- }
怎么样,三种C# 遍历CheckBox方法都很简单把,希望能给大家带来帮助。
【编辑推荐】