更新时间:2023-11-24 14:40:40
它不起作用,因为列表是空的。试试这个:
私人无效的button1_Click(对象发件人,EventArgs的发送)
{
清单<按钮和GT;按钮=新的List<按钮和GT;();
的for(int i = 0;我小于10;我++)
{
按钮newButton =新按钮();
buttons.Add(newButton);
this.Controls.Add(newButton);
}
}
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]);
}
}
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);
}
}