Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C中的for循环将复选框指定给复选框数组#_C#_Arrays_Loops_For Loop_Checkbox - Fatal编程技术网

C# 使用C中的for循环将复选框指定给复选框数组#

C# 使用C中的for循环将复选框指定给复选框数组#,c#,arrays,loops,for-loop,checkbox,C#,Arrays,Loops,For Loop,Checkbox,我想这样做: CheckBox[] boxarray = new CheckBox[4]; boxarray[0] = checkBox1; boxarray[1] = checkBox2; boxarray[2] = checkBox3; boxarray[3] = checkBox4; 使用for循环-因为我有大约600个复选框。我想了想: for (int i = 0 ; i<= 600; i++) { boxes[i] = chec

我想这样做:

CheckBox[] boxarray = new CheckBox[4];
    boxarray[0] = checkBox1;
    boxarray[1] = checkBox2;
    boxarray[2] = checkBox3;
    boxarray[3] = checkBox4;
使用for循环-因为我有大约600个复选框。我想了想:

for (int i = 0 ; i<= 600; i++)
    {
    boxes[i] = checkBox[i] ;
    }

for(int i=0;i您不能像以前那样指定代码中复选框的名称,因为
i
不会更改

您必须创建复选框的新实例,如:

for (int i = 0 ; i<= 600; i++)
{
    //This will create a new instance of a CheckBox
    CheckBox cb = new CheckBox()

    //At this point the checkbox is empty and not visible anywhere
    //So you have to change some properties:
    cb.Text = "Check me!";
    cb.Top = Ycoord; //you should change this depending on i like +(i*30)
    cb.Left = Xcoord;
    cb.name = "checkbox" + i.toString();
    //To recognize if one of your Checkboxes is checked/unchecked you need to add
    //an event like this (for the event see down below)
    cb.CheckedChanged += checkedChangedEvent;
    //then you need to add the checkbox to your Array
    boxes[i] = cb;
    //to make those checkboxes visible you need to add them to your form
    //or Panel:
    myForm.Controls.Add(cb);
}
您必须通过其名称指定选中/取消选中的复选框,因为每个复选框都触发相同的事件

编辑2:

要获取复选框的编号,您可以在事件中执行以下操作:

int cbNumber = 0;
try
{
    cbNumber = System.Convert.toInt32(((CheckBox)sender).name.Replace("checkbox",""));
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message + "\n\n" + ex.StackTrace);
}

使用当前设置,您必须使用FindControl(假设ASP.net、winforms和WPF的工作方式不同):

每次它都返回正确的计数,所以我认为这将非常适合 你在找什么:)

如果表单上有其他不想添加的复选框,则必须使用以下方法:

for(int i = 0; i <= 600; i++)
{
    boxes[i] = this.Controls.Find("checkbox" + i, true).FirstOrDefault() as CheckBox;
}

for(int i=0;i如果表单上有这些复选框,则可以将它们添加到数组(或列表,这样做会容易得多)中,如下所示:

列表复选框列表=新列表();
foreach(FormName.Controls中的控件ctrl)
{
if(ctrl.GetType()==typeof(复选框))
{
复选框列表。添加(ctrl作为复选框);
}
}
记住,简单地写下:
checkBox1
checkBox2


无法像复选框[0]那样获取它们
-它不是一个数组,它只是一个名称,结尾是“1”或“2”。

您是否在Visual Studio designer中创建这些复选框?因为我有大约600个复选框,我想看看UI的外观。您的意思是您有大约600个条目要放在一个复选框中?还是您真的想创建600个复选框?对我来说似乎太极端了!!!这是一个web表单应用程序吗?winforms?wpf?什么是
框[]
?什么是
框[]
?事实上,我没有。我只是假设。我没有逻辑上的理由假设它是ASP.NET,除了它是我知道如何最好地解释它的技术之外。但概念仍然是一样的:他必须将他的计数器连接到始终保持不变的部分,并手动查找它们。自这就是OP的用途。我也读过。但事实上我以前从未这样做过,所以我甚至不确定这会是什么样子。也许你可以解释一下它的作用,请?添加更多细节;)
for(int i = 0; i <= 600; i++)
{
    boxes[i] = Page.FindControl("checkbox" + i);
}
public IEnumerable<Control> GetAll(Control control,Type type) {
    var controls = control.Controls.Cast<Control>();
    return controls.SelectMany(ctrl => GetAll(ctrl,type))
                   .Concat(controls)
                   .Where(c => c.GetType() == type); 
}
private void Form1_Load(object sender, EventArgs e) {
    var c = GetAll(this,typeof(TextBox));
    MessageBox.Show("Total Controls: " + c.Count());
}
for(int i = 0; i <= 600; i++)
{
    boxes[i] = this.Controls.Find("checkbox" + i, true).FirstOrDefault() as CheckBox;
}