How to add buttons dynamically to my form?

2019-01-14 07:26发布

I want to create 10 buttons on my form when I click on button1. No error with this code below but it doesnt work either.

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

8条回答
Fickle 薄情
2楼-- · 2019-01-14 07:45

It doesn't work because the list is empty. Try this:

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);   
    }
}
查看更多
倾城 Initia
3楼-- · 2019-01-14 07:48

You can't add a Button to an empty list without creating a new instance of that Button. You are missing the

Button newButton = new Button();  

in your code plus get rid of the .Capacity

查看更多
Fickle 薄情
4楼-- · 2019-01-14 07:50

use button array like this.it will create 3 dynamic buttons bcoz h variable has value of 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]);

}  }
查看更多
爷的心禁止访问
5楼-- · 2019-01-14 07:51

You could do something like this:

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

If you want them to layout in any sort of reasonable fashion it would be better to add them to one of the layout panels (i.e. FlowLayoutPanel) or to align them yourself.

查看更多
混吃等死
6楼-- · 2019-01-14 08:01

I came with the same doubt, with the current contribution on the issue I could came up with:

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

                }

            }

The expected behaviour is that, this will generate buttons using the current state of your window size, Always breaking a line when the next button would exceed the right margin of your window.

查看更多
时光不老,我们不散
7楼-- · 2019-01-14 08:04

First, you aren't actually creating 10 buttons. Second, you need to set the location of each button, or they will appear on top of each other. This will do the trick:

  for (int i = 0; i < 10; ++i)
  {
      var button = new Button();
      button.Location = new Point(button.Width * i + 4, 0);
      Controls.Add(button);
  }
查看更多
登录 后发表回答