动态创建控件

时间:2009-01-11 22:10:43

标签: c#

        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)
{

}

如何创建复选框的新实例?因为这样我每次只能获得一个复选框。我希望每行有三个复选框。

5 个答案:

答案 0 :(得分:3)

这是winforms吗?第一点:您每次都不需要new Control()(当您new CheckBox()时,您无论如何都要丢弃它。您希望布局如何显示?您能再描述一下吗?

我想TableLayoutPanel可能是一个合理的开始......

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

否则,您需要手动设置每个Location(或TopLeft);不简单。

答案 1 :(得分:2)

您的代码有问题。让我们从示例代码而不是课程开始。我将首先创建一个Panel,如果你想删除你创建的复选框,那就太好了。您可能对用户单击复选框感兴趣,所以我们为此添加一个事件。启动一个新的WF项目并在表单上放置一个按钮。双击它,然后粘贴此代码:

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

答案 2 :(得分:1)

看起来你得到了200个实例,所有实例都放在同一点上。

答案 3 :(得分:0)

在循环体内实例化3个新复选框,相应地设置它们的属性并将它们中的每一个添加到列表中。完成上述代码后,您将拥有600个复选框。

list.Add(对照1);

list.Add(控制2);

list.Add(CONTROL3);

答案 4 :(得分:0)

我不确定你要做什么,但我稍微清理了你的代码:

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

如果要添加刚刚创建的控件,则不应向列表中添加新实例。

此外:

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

有点多余。另外,为了不多次在同一地点获得一个控制,你应该改变这一点。希望这有帮助