2011-12-22 51 views
13

我想在窗体上创建10个按钮,当我点击button1。下面的代码没有错误,但它也不起作用。如何动态添加按钮到我的表单?

private void button1_Click(object sender, EventArgs e) 
{ 
    List<Button> buttons = new List<Button>(); 
    for (int i = 0; i < buttons.Capacity; i++) 
    { 
     this.Controls.Add(buttons[i]); 
    } 
} 
+0

您必须指定高度和宽度,否则它们将不可见 – 2011-12-22 18:36:31

+0

并且您再次得到有效答案,但您尚未接受它或解释为什么它不合适... – Adam 2011-12-22 19:25:11

回答

17

它不工作,因为列表为空。试试这个:

private void button1_Click(object sender, EventArgs e) 
{ 
    List<Button> buttons = new List<Button>(); 
    for (int i = 0; i < 10; i++) 
    { 
     Button newButton = new Button(); 
     buttons.Add(newButton); 
     this.Controls.Add(newButton); 
    } 
} 
3

你可以做这样的事情:

Point newLoc = new Point(5,5); // Set whatever you want for initial location 
for(int i=0; i < 10; ++i) 
{ 
    Button b = new Button(); 
    b.Size = new Size(10, 50); 
    b.Location = newLoc; 
    newLoc.Offset(0, b.Height + 5); 
    Controls.Add(b); 
} 

如果你希望他们在任何一种合理的方式布局倒不如将它们添加到布局面板之一(即FlowLayoutPanel)或自己调整它们。

0

如果不创建该按钮的新实例,则无法将按钮添加到空列表。 你缺少

Button newButton = new Button(); 

在代码中加摆脱.Capacity

17

没有创建任何按钮,你只要有一个空的列表。

您可以忘记列表并只在循环中创建按钮。

private void button1_Click(object sender, EventArgs e) 
{  
    int top = 50; 
    int left = 100; 

    for (int i = 0; i < 10; i++)  
    {   
      Button button = new Button(); 
      button.Left = left; 
      button.Top = top; 
      this.Controls.Add(button);  
      top += button.Height + 2; 
    } 
} 
+0

+1一个可靠的解决方案 – Adam 2011-12-22 18:57:32

0

首先,您实际上并没有创建10个按钮。其次,您需要设置每个按钮的位置,否则它们将显示在对方的顶部。这将做到这一点:

for (int i = 0; i < 10; ++i) 
    { 
     var button = new Button(); 
     button.Location = new Point(button.Width * i + 4, 0); 
     Controls.Add(button); 
    } 
1

两个问题 - 列表为空。您需要先将一些按钮添加到列表中。第二个问题:你不能将按钮添加到“this”。 “这个”并不是参照你的想法,我想。例如,将其更改为引用Panel。

//Assume you have on your .aspx page: 
<asp:Panel ID="Panel_Controls" runat="server"></asp:Panel> 


private void button1_Click(object sender, EventArgs e) 
    { 
     List<Button> buttons = new List<Button>(); 


     for (int i = 0; i < buttons.Capacity; i++) 
     { 
      Panel_Controls.Controls.Add(buttons[i]); 
     } 
    } 
+0

在这种情况下,'this'是对他的'Form'的引用 – Adam 2011-12-22 18:56:57

0

使用按钮配置像this.it将创建bcoz^h变3个动态按钮有3

private void button1_Click(object sender, EventArgs e) 
{ 
int h =3; 


Button[] buttonArray = new Button[8]; 

for (int i = 0; i <= h-1; i++) 
{ 
    buttonArray[i] = new Button(); 
    buttonArray[i].Size = new Size(20, 43); 
    buttonArray[i].Name= ""+i+""; 
    buttonArray[i].Click += button_Click;//function 
    buttonArray[i].Location = new Point(40, 20 + (i * 20)); 
    panel1.Controls.Add(buttonArray[i]); 

} } 
0

值我带着同样的疑问,对我能来到这个问题目前贡献了:

int altura = this.Size.Height; 
      int largura = this.Size.Width; 

      int alturaOffset = 10; 
      int larguraOffset = 10; 
      int larguraBotao = 100; //button widht 
      int alturaBotao = 40; //button height 

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

       if ((larguraOffset+larguraBotao) >= largura) 
       {      
        larguraOffset = 10; 
        alturaOffset = alturaOffset + alturaBotao; 
        var button = new Button(); 
        button.Size = new Size(larguraBotao, alturaBotao); 
        button.Name = "" + i + ""; 
        button.Text = "" + i + ""; 
        //button.Click += button_Click;//function 
        button.Location = new Point(larguraOffset, alturaOffset); 
        Controls.Add(button); 
        larguraOffset = larguraOffset + (larguraBotao); 
       } 
       else 
       { 

        var button = new Button(); 
        button.Size = new Size(larguraBotao, alturaBotao); 
        button.Name = "" + i + ""; 
        button.Text = "" + i + ""; 
        //button.Click += button_Click;//function 
        button.Location = new Point(larguraOffset, alturaOffset); 
        Controls.Add(button); 
        larguraOffset = larguraOffset+(larguraBotao); 

       } 

      } 

预期的行为是,这将使用窗口大小的当前状态,总是打破了线生成按钮时,下一个按钮会超出正确的MARGI n你的窗户。

+0

什么是larguraBotao和alturaBotao的意思 – 2017-08-03 07:08:48