dotnet knowhow
dotnet knowhow

Reputation:

Dynamic creation of control

        for (int i = 0; i < 200; i++)
        {

            Control control = new Control();

            control = new CheckBox();
            Size size = control.Size;
            Point point = new Point(20, 22);
            control.Location = point; 
            int width = size.Width + 5;
            i += width;
            list.Add(control);
        }

foreach(Control c in list)
{

}

how do I create a new instance of checkbox? Because this way I am getting just one checkbox each time. I want to get three checkbox in each row.

Upvotes: 0

Views: 1317

Answers (5)

Marc Gravell
Marc Gravell

Reputation: 1064064

Is this winforms? A first point: you don't need the new Control() each time (you simly discard it anyway when you new CheckBox(). How exactly do you want the layout to appear? Can you describe it a bit more please?

I imagine TableLayoutPanel might be a reasonable start...

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Form form = new Form();
    TableLayoutPanel layout = new TableLayoutPanel();
    layout.Dock = DockStyle.Fill;
    form.Controls.Add(layout);
    layout.AutoScroll = true;
    layout.ColumnCount = 3;
    // size the columns (choice just to show options, not to be pretty)
    layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 200));
    layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
    layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));    
    layout.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
    for (int i = 0; i < 200; i++)
    {
        CheckBox chk = new CheckBox();
        chk.Text = "item " + i;
        layout.Controls.Add(chk);
    }

    Application.Run(form);
}

Otherwise, you'll need to manually set the Location (or Top and Left) of each; not simple.

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 942267

Your code has problems. Let's work from sample code rather than a lesson. I'll create a Panel first, nice if you want to remove the checkboxes you created. You'd probably be interested in the user clicking a checkbox so lets add an event for that. Start a new WF project and drop a button on the form. Double click it, then paste this code:

private void button1_Click(object sender, EventArgs e) {
  // Give the 3 checkboxes a decent spacing
  int height = this.Font.Height * 3 / 2;
  // Create the panel first, add it to the form
  Panel pnl = new Panel();
  pnl.Size = new Size(100, 3 * height);
  pnl.Location = new Point(10, 5);
  this.Controls.Add(pnl);
  // Make three checkboxes now
  for (int ix = 0; ix < 3; ++ix) {
    CheckBox box = new CheckBox();
    box.Size = new Size(100, height);
    // As pointed out, avoid overlapping them
    box.Location = new Point(0, ix * height);
    box.Text = "Option #" + (ix + 1).ToString();
    box.Tag = ix;
    // We want to know when the user checked it
    box.CheckedChanged += new EventHandler(box_CheckedChanged);
    // The panel is the container
    pnl.Controls.Add(box);
  }
}

void box_CheckedChanged(object sender, EventArgs e) {
  // "sender" tells you which checkbox was checked
  CheckBox box = sender as CheckBox;
  // I used the Tag property to store contextual info, just the index here
  int index = (int)box.Tag;
  // Do something more interesting here...
  if (box.Checked) {
    MessageBox.Show(string.Format("You checked option #{0}", index + 1));
  }
}

Upvotes: 2

Matthias van der Vlies
Matthias van der Vlies

Reputation: 3922

I am not sure about what you are trying to do, but I cleaned up your code a bit:

for (int i = 0; i < 200; i++)
{

    Control control = new CheckBox();

    control.Location = new Point(20, 22); 
    i += control.Size.Width + 5;

    list.Add(control);
}

You should not add a new instance to the list if you want to add the control you just made.

Also:

Control control = new Control();
control = new CheckBox();

Is a bit redundant. Also to not get one control at the same spot multiple times you should alter the point. Hope this helps

Upvotes: 0

&#216;yvind Skaar
&#216;yvind Skaar

Reputation: 1840

It looks like you get your 200 instances, all placed at the same point.

Upvotes: 1

Jobo
Jobo

Reputation: 916

Instantiate 3 new checkboxes inside your loop body, set their properties accordingly and add each of them to the list. After the code above is complete, you will have 600 checkboxes.

list.Add(Control1);

list.Add(Control2);

list.Add(Control3);

Upvotes: 0

Related Questions