Using the current setup, you'll have to use FindControl (assuming ASP.net, winforms and WPF work differently):
for(int i = 0; i <= 600; i++)
{
boxes[i] = Page.FindControl("checkbox" + i);
}
Update for windows forms: There is actually a method you can use to find all controls of a specific type:
How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
Here's another option for you. I tested it by creating a sample
application, I then put a GroupBox and a GroupBox inside the initial
GroupBox. Inside the nested GroupBox I put 3 TextBox controls and a
button. This is the code I used (even includes the recursion you were
looking for)
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);
}
To test it in the form load event I wanted a count of all controls inside
the initial GroupBox
private void Form1_Load(object sender, EventArgs e) {
var c = GetAll(this,typeof(TextBox));
MessageBox.Show("Total Controls: " + c.Count());
}
And it returned the proper count each time, so I think this will work perfectly for
what you're looking for :)
If you have other checkboxes on your form that you don't want to add, you'll have to use the following method:
for(int i = 0; i <= 600; i++)
{
boxes[i] = this.Controls.Find("checkbox" + i, true).FirstOrDefault() as CheckBox;
}