如何每次点击按钮创建一个新按钮并放入面板?。
我不知道用什么方法来创建按钮数组。
private void button1_Click(object sender, EventArgs e)
{
Button c = new Button();
c.Location = new Point(15,40);
c.Text = "novo";
panel1.Controls.Add(c);
}
答案 0 :(得分:1)
您可以创建如下按钮列表
public partial class Form1 : Form
{
List<Button> ButtonList = new List<Button>();
然后你可以像你一样创建按钮
private void button1_Click(object sender, EventArgs e)
{
Button c = new Button();
c.Location = new Point(10 , 40);
c.Text = "novo";
ButtonList.Add(c); // add to list as well
panel1.Controls.Add(c);
}
请注意,您可能希望更改每个按钮的位置,否则所有按钮都会重叠,您只会看到一个位于顶部的按钮
答案 1 :(得分:1)
您可以创建新按钮并将其添加到面板,只需在同一位置创建它们。
c.Location = new Point(15,40);
您可能需要在班级上为X或Y坐标或两者提供一些计数器。
public class Form1 : FOrm {
private int x = 15;
private void button1_Click(object sender, EventArgs e)
{
Button c = new Button();
c.Location = new Point(x,40);
c.Text = "novo";
panel1.Controls.Add(c);
x += 10 + c.Size.Width;
}
}
您可能想要检查自己是否超出了表单的界限,并从“新行”的开头开始。