使用C#中的for循环将复选框分配给复选框数组 [英] Assign checkboxes to an checkbox-array using a for loop in C#
本文介绍了使用C#中的for循环将复选框分配给复选框数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想这样做:
CheckBox[] boxarray = new CheckBox[4];
boxarray[0] = checkBox1;
boxarray[1] = checkBox2;
boxarray[2] = checkBox3;
boxarray[3] = checkBox4;
使用for循环-因为我有大约600个复选框.我想到了:
using a for loop - because I have about 600 checkboxes. I thought about this:
for (int i = 0 ; i<= 600; i++)
{
boxes[i] = checkBox[i] ;
}
但是无论我尝试了什么-都行不通.我希望你明白我的意思. 感谢您的帮助!
But whatever I tried - it didn't work. I hope you know what I mean. Thanks for your help!
推荐答案
如果您的表单上有这些CheckBox,则可以将它们添加到数组(或List,这样做会更容易),如下所示:
If you have these CheckBoxes on your form, then you can add them to an array (or List, which would be much easier to do) like that:
List<CheckBox> checkBoxesList = new List<CheckBox>();
foreach (Control ctrl in FormName.Controls)
{
if (ctrl.GetType() == typeof(CheckBox))
{
checkBoxesList.Add(ctrl as CheckBox);
}
}
记住,只需写:
checkBox1
,checkBox2
不可能像checkBox[0]
那样获得它们-它不是数组,而只是一个以'1'或'2'结尾的名称.
Remember, simply writing:
checkBox1
, checkBox2
won't make it possible to get them like checkBox[0]
- it's not an array, it's just a name with '1' or '2' in the end.
这篇关于使用C#中的for循环将复选框分配给复选框数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文