//方法1
private void button1_Click(object sender, EventArgs e)
{
//遍历groupBox里的所有控件:Comtrol
foreach (Control c in groupBox3.Controls)
{//判断当前Comtrol是不是Checkbox控件?
if (c is CheckBox)
{//是,转换类型为Checkbox控件
CheckBox ck = c as CheckBox;
//判断Checkbox控件是不是checked?
if (ck.Checked)
{//是,取Text文本
this.groupBox3.Text += ShowChecked(ck);
}
}
}
}
private string ShowChecked(CheckBox ck) //返回string类型
{ //字符串构造器
StringBuilder sb = new StringBuilder();
if (ck.Checked)
{ //自动带回车换行符
sb.AppendLine(ck.Text);//没选中返回空
}
return sb.ToString(); //返回字符串
}
//方法2
private void button1_Click(object sender, EventArgs e)
{
DealCheckboxes(groupBox3, groupBox3);
}
public void DealCheckboxes(Control container,Control contorlToDisplay)
{
contorlToDisplay.Text = "";
foreach (Control c in container.Controls)
{
if (c is CheckBox)
{
contorlToDisplay.Text += ShowChecked(c as CheckBox);
}
}
}
private string ShowChecked(CheckBox ck) //返回string类型
{ //字符串构造器
StringBuilder sb = new StringBuilder();
if (ck.Checked)
{ //自动带回车换行符
sb.AppendLine(ck.Text);//没选中返回空
}
return sb.ToString();
}
原文地址:http://blog.51cto.com/gongxi/2117271
时间: 2024-10-12 00:55:58