用C# Winform 处理100多个checkbox 然后要返回checkbox.Text 给 textbox.Text 由于选择是不固定的 怎么处理呢 虽然也可以用比较笨的办法如下:
//string a = "", b="", c="", d="",m="", f="", g="", h="", i="", j=""; //if(checkBox1.Checked)
//{
// a = Convert.ToString(checkBox1.Text +" ");
//}
//if(checkBox2.Checked)
//{
// b = Convert.ToString(checkBox2.Text+" ") ;
//}
//if(checkBox3.Checked)
//{
// c = Convert.ToString(checkBox3.Text + " ");
//}
//if (checkBox4.Checked)
//{
// d = Convert.ToString(checkBox4.Text + " ");
//}
//if (checkBox5.Checked)
//{
// m = Convert.ToString(checkBox5.Text + " ");
//}
//if (checkBox6.Checked)
//{
// f = Convert.ToString(checkBox6.Text + " ");
//}
//if (checkBox7.Checked)
//{
// g = Convert.ToString(checkBox7.Text + " ");
//}
//if (checkBox8.Checked)
//{
// h = Convert.ToString(checkBox8.Text + " ");
//}
//if (checkBox11.Checked)
//{
// i = Convert.ToString(checkBox11.Text + " ");
//}
//if (checkBox10.Checked)
//{
// j = Convert.ToString(checkBox10.Text + " ");
//}
//txtQu.Text = string.Format(a + b + c + d + m + f + g + h + i + j);但是要处理100多个那就他麻烦了 还有什么比较简单的处理方法啊?? 比如说循环处理???各位帮帮忙啊
//string a = "", b="", c="", d="",m="", f="", g="", h="", i="", j=""; //if(checkBox1.Checked)
//{
// a = Convert.ToString(checkBox1.Text +" ");
//}
//if(checkBox2.Checked)
//{
// b = Convert.ToString(checkBox2.Text+" ") ;
//}
//if(checkBox3.Checked)
//{
// c = Convert.ToString(checkBox3.Text + " ");
//}
//if (checkBox4.Checked)
//{
// d = Convert.ToString(checkBox4.Text + " ");
//}
//if (checkBox5.Checked)
//{
// m = Convert.ToString(checkBox5.Text + " ");
//}
//if (checkBox6.Checked)
//{
// f = Convert.ToString(checkBox6.Text + " ");
//}
//if (checkBox7.Checked)
//{
// g = Convert.ToString(checkBox7.Text + " ");
//}
//if (checkBox8.Checked)
//{
// h = Convert.ToString(checkBox8.Text + " ");
//}
//if (checkBox11.Checked)
//{
// i = Convert.ToString(checkBox11.Text + " ");
//}
//if (checkBox10.Checked)
//{
// j = Convert.ToString(checkBox10.Text + " ");
//}
//txtQu.Text = string.Format(a + b + c + d + m + f + g + h + i + j);但是要处理100多个那就他麻烦了 还有什么比较简单的处理方法啊?? 比如说循环处理???各位帮帮忙啊
再操作。
string Str="";
foreach (Control MyControl in this.Controls)//遍历当前窗体下所有的控件
{
//如果是CheckBox
if (MyControl.GetType().ToString() == "System.Windows.Forms.CheckBox")
{
if(MyControl.Checked)
{Str=Str+MyControl.Text;}
}
}
txtQu.Text =Str;
foreach (Control MyControl in this.Controls)//遍历当前窗体下所有的控件
{
//如果是CheckBox
if (MyControl is CheckBox)
{
if ((MyControl as CheckBox).Checked)
{
Str += MyControl.Text + " ";
}
}
}
错误 1 “System.Windows.Forms.Control”并不包含“Checked”的定义
string Str = "";
foreach (Control ctr in this.Controls) if (ctr.GetType() ==typeof(System.Windows.Forms.CheckBox) )
if(((System.Windows.Forms.CheckBox)ctr).Checked)Str += ctr.Text + " ";
int k = 15;
int w = 40;
CheckBox[] ck = new CheckBox[list2.Count];
for (int i = 0; i < list2.Count; i++)
{
#region 添加一级标题
ck[i] = new CheckBox();
ck[i].Name = "ck" + i;
ck[i].ForeColor = Color.Red;
ck[i].AutoSize = true;
ck[i].Tag = list2[i].NodeId;
ck[i].Text = list2[i].DisplayName;
ck[i].Location = new Point(k, w);
ck[i].Click += new EventHandler(frmRole_Click);
this.Controls.Add(ck[i]);
#endregion
#region 添加二级标题
List<SysFunData> list3 = selectSysFunInfoBll.selectSecondLevelMenu(list2[i].NodeId,saveValue.LoginInfo.RoleId);
CheckBox[] ck2 = new CheckBox[list3.Count];
for (int j = 0; j < list3.Count; j++)
{
ck2[j] = new CheckBox();
ck2[j].Text = list3[j].DisplayName;
ck2[j].Name = "ck" + i + j;
ck2[j].ForeColor = Color.Black;
ck2[j].AutoSize = true;
ck2[j].Tag = list3[j].NodeId;
ck2[j].Location = new Point(k + j * 80, w + 20);
ck2[j].Click += new EventHandler(frmRole_Click2);
this.Controls.Add(ck2[j]);
}
#endregion
w += 50;
}
然后用动态方式添加事件即可
//[递归,不管楼主放到哪里,都可以取到]
private void Test_Click(object sender, EventArgs e)
{
CheckBoxSelect(this);
} //[ 定义取得文本的字符串 ]
string sCheckBoxTest;
//[递归取得所有CheckBox的文本 ]
private void CheckBoxSelect(Control c)
{
if (c.Controls.Count > 0)
{
foreach (Control item in c.Controls)
{
if (item is CheckBox)
{
CheckBox txt = (TextBox)CheckBox;
sCheckBoxTest+=txt }
CheckBoxSelect(item);
}
}
}最终sCheckBoxTest中保存的就是楼主需要的