C# Windows窗体按钮调用按钮
如何让每次单击按钮都创建一个新按钮并放入面板 我不知道使用什么方法来创建按钮数组C# Windows窗体按钮调用按钮,c#,forms,button,panel,C#,Forms,Button,Panel,如何让每次单击按钮都创建一个新按钮并放入面板 我不知道使用什么方法来创建按钮数组 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); } 您可以创建如下所示
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);
}
您可以创建如下所示的按钮列表
public partial class Form1 : Form
{
List<Button> ButtonList = new List<Button>();
请注意,您可能需要更改每个按钮的位置,否则所有按钮都会重叠,您只会看到顶部的一个按钮您可以创建新按钮并将其添加到面板中,只需在同一位置创建它们即可
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;
}
}
您可能希望检查您是否超出了形式的界限,并从“新行”的开头开始。通过在标题旁边添加,您表示问题已结束,在您的情况下,我相信您的意思是问题已得到回答。这就是绿色复选标记的含义。
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;
}
}