2011-12-22 45 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