gpt4 book ai didi

c# - 如何向我的表单动态添加按钮?

转载 作者:IT王子 更新时间:2023-10-29 04:52:26 26 4
gpt4 key购买 nike

我想在单击 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]);
}
}

最佳答案

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

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

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

关于c# - 如何向我的表单动态添加按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8608311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com