代码:
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
//遍历窗体上所有控件
foreach (Control ctr in this.Controls)
{
//判断该控件是不是CheckBox
if (ctr is CheckBox)
{
//将ctr转换成CheckBox并赋值给ck
CheckBox ck = ctr as CheckBox;
if (ck.Checked)
{
sb.AppendLine(ck.Text);
}
}
}
this.label2.Text = sb.ToString();
}