0

I'd like to do this:

CheckBox[] boxarray = new CheckBox[4];
    boxarray[0] = checkBox1;
    boxarray[1] = checkBox2;
    boxarray[2] = checkBox3;
    boxarray[3] = checkBox4;

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!

leogoettke
  • 33
  • 1
  • 6
  • 1
    Are you creating these CheckBoxes in Visual Studio designer? – Bartłomiej Zieliński Sep 01 '14 at 11:59
  • 9
    *because I have about 600 checkboxes* I'd love to see how the UI looks. – Sriram Sakthivel Sep 01 '14 at 12:00
  • do you mean you have ~600 entries to be placed in a single checkbox? or you actually want to create **600** checkboxes? Seems pretty extreme to me!!! – jbutler483 Sep 01 '14 at 12:02
  • is this a web forms application? winforms? wpf? – Andrej Kikelj Sep 01 '14 at 12:03
  • What is `boxes[]`? What is `checkBox[]`? – David Sep 01 '14 at 12:03
  • please make your question more clear?..its not exact from your question – Lijo Sep 01 '14 at 12:05
  • Okay. Yes, it seems weird but I really have 600 checkboxes. By the way: I am using Visual Studio Express 2013 for Windows Desktop and it is a Windows Form Application. I would like to connect the checkboxes to a mysql database and thought it would be easier if I was creating a checkbox-array. Please don't ask for details. As you may see, I am no expert but I think this array could it make easier. Not to have to assign one checkbox to an array's item each, I thought about the for loop. – leogoettke Sep 01 '14 at 12:19

3 Answers3

0

you are not able to specifiy the name of the checkboxes in the code like you have done, because i wont get changed.

you have to create new instances of CheckBox like:

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);
}

The Event:

public void checkedChangedEvent(Object sender, EventArgs e)
{
    MessageBox.Show(((CheckBox)sender).name + " is now Checked == " + ((CheckBox)sender).Checked.toString();
}

you have to specify which Checkbox was Checked/Unchecked by its name, because every CheckBox is firing the same event.

EDIT2:

to get the number of the checkbox you can do something like this in the event:

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

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;
}
Community
  • 1
  • 1
Nzall
  • 3,439
  • 5
  • 29
  • 59
  • I don't, actually. I just assumed it. I have no logical reason to assume that it's ASP.NET aside from it being the technology I know how to explain it in best. The concept remains the same though: he'll have to concatenate his counter to the part that always stays the same and look them up manually. – Nzall Sep 01 '14 at 12:09
  • Updated with Winform methodology since that's what the OP uses. – Nzall Sep 01 '14 at 12:36
0

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);
    }
}

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.