【问题标题】:How to add buttons dynamically to my form?如何将按钮动态添加到我的表单中?
【发布时间】:2012-01-26 08:33:14
【问题描述】:

当我点击 button1 时,我想在我的表单上创建 10 个按钮。下面这段代码没有错误,但它也不起作用。

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

【问题讨论】:

  • 您必须指定高度和宽度,否则它们将不可见
  • 又一次你得到了一个有效的答案,但你还没有接受它或解释它为什么不适合......

标签: c# winforms button dynamic


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    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)或自己对齐它们。

      【讨论】:

        【解决方案4】:

        两个问题 - 列表为空。您需要先将一些按钮添加到列表中。第二个问题:您不能向“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]);   
                }
            }
        

        【讨论】:

        • 在这种情况下,this 是对他的Form 的引用
        【解决方案5】:

        像这样使用按钮数组。它将创建 3 个动态按钮 bcoz h 变量的值为 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]);
        
        }  }
        

        【讨论】:

          【解决方案6】:

          我有同样的疑问,想出了以下贡献:

           int height = this.Size.Height;
           int width = this.Size.Width;
          
           int widthOffset = 10;
           int heightOffset = 10;
          
           int btnWidth = 100;  // Button Widht
           int btnHeight = 40;  // Button Height
          
           for (int i = 0; i < 50; ++i)
           {
               if ((widthOffset + btnWidth) >= width)
               {                    
                   widthOffset = 10;
                   heightOffset = heightOffset + btnHeight
          
                   var button = new Button();
                   button.Size = new Size(btnWidth, btnHeight);
                   button.Name = "" + i + "";
                   button.Text = "" + i + "";
                   //button.Click += button_Click; // Button Click Event
                   button.Location = new Point(widthOffset, heightOffset);
          
                   Controls.Add(button);
          
                   widthOffset = widthOffset + (btnWidth);
               }
          
               else
               {                        
                   var button = new Button();
                   button.Size = new Size(btnWidth, btnHeight);
                   button.Name = "" + i + "";
                   button.Text = "" + i + "";
                   //button.Click += button_Click; // Button Click Event
                   button.Location = new Point(widthOffset, heightOffset);
          
                   Controls.Add(button);
          
                   widthOffset = widthOffset + (btnWidth);
                }
            }
          

          预期行为:
          这将动态生成按钮并使用当前窗口大小,当按钮超出窗口的右边距时“换行”。

          【讨论】:

          • larguraBotao 和 alturaBotao 是什么意思
          【解决方案7】:

          首先,您实际上并没有创建 10 个按钮。其次,您需要设置每个按钮的位置,否则它们将出现在彼此之上。这样就可以了:

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

          【讨论】:

            【解决方案8】:

            如果不创建该 Button 的新实例,则无法将 Button 添加到空列表中。 你错过了

            Button newButton = new Button();  
            

            在你的代码中加上去掉.Capacity

            【讨论】:

              猜你喜欢
              • 2015-07-29
              • 2018-09-05
              • 2020-02-12
              • 2023-03-15
              • 2010-11-23
              • 1970-01-01
              • 1970-01-01
              • 2016-05-07
              • 1970-01-01
              相关资源
              最近更新 更多