Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Adding Checkboxes to a Windows Form Only Shows one Checkbox

I'm sorry if this seems n00bish, but I have been searching for this for a few days now. I am attempting to dynamically add checkboxes to a windows form; however, only one checkbox appears on the form. Here is my code:

for (int i = 0; i < 10; i++)
{
    box = new CheckBox();
    box.Tag = i.ToString();
    box.Text = "a";
    box.AutoSize = true;
    box.Location = new Point(10, i + 10);
    Main.Controls.Add(box);
}

As you can see I am adding the checkboxes via a for loop. I have tried messing with the location and enabling autosize in case they were somehow overlapping. The result is a single checkbox with text "a".

like image 628
Chris Ruskai Avatar asked Feb 21 '13 14:02

Chris Ruskai


2 Answers

Actually you already created a CheckBox but within the same point.

CheckBox box;
for (int i = 0; i < 10; i++)
{
    box = new CheckBox();
    box.Tag = i.ToString();
    box.Text = "a";
    box.AutoSize = true;
    box.Location = new Point(10, i * 50); //vertical
    //box.Location = new Point(i * 50, 10); //horizontal
    this.Controls.Add(box);
}
like image 94
spajce Avatar answered Nov 12 '22 02:11

spajce


In this case with help of dynamically assign Name property how to achive checkbox.checked property , in some other action like submit button. how can i get all check box is checked and which is created in loop?

like image 42
Sheela K R Avatar answered Nov 12 '22 04:11

Sheela K R