动态添加到表单的按钮不会出现

Luk*_* M. 0 c# button winforms

当我点击时button1,我正在尝试在表单上创建4个按钮,但按钮不会显示.为什么不?

private void button1_Click(object sender, EventArgs e)
{
   Button[] b = new Button[4];
   for (int i=0; i < 4; i++)
   {
      b[i] = new Button();
      b[i].Name = "button" + i;
      b[i].Location = new Point(43, 39 + 10 * i);
      b[i].Size = new Size(158, 48);
   }
}
Run Code Online (Sandbox Code Playgroud)

Zbi*_*iew 5

您只创建了它们,但您还需要将它们添加到表单中: this.Controls.Add(b[i]);

private void button1_Click(object sender, EventArgs e)
{
   Button[] b = new Button[4];
   for (int i=0; i < 4; i++)
   {
       b[i] = new Button();
       b[i].Name = "button" + i;
       b[i].Location = new Point(43, 39 + 10 * i);
       b[i].Size = new Size(158, 48);

       this.Controls.Add(b[i]);
   }
}
Run Code Online (Sandbox Code Playgroud)