How to add buttons dynamically to my form?

前端 未结 8 1874
盖世英雄少女心
盖世英雄少女心 2020-12-01 12:34

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 se         


        
8条回答
  •  無奈伤痛
    2020-12-01 13:02

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

提交回复
热议问题