【问题标题】:Windows Form button call buttonWindows 窗体按钮调用按钮
【发布时间】:2013-06-05 11:11:33
【问题描述】:

如何让每次点击按钮创建一个新按钮并放入面板?

我不知道用什么方法来创建按钮数组。

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


    }

【问题讨论】:

  • 通过在您的标题后面加上 close ,表示您的问题已关闭,在您的情况下,我相信您的意思是它已被回答。这就是绿色复选标记的用途。

标签: c# forms button panel


【解决方案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;
    }
}

您可能想检查您是否超出表格边界并从“新行”的开头开始。

【讨论】:

    【解决方案2】:

    您可以创建如下按钮列表

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

    请注意,您可能需要更改每个按钮的位置,否则所有按钮都会重叠,您只会看到一个位于顶部的按钮

    【讨论】:

      猜你喜欢
      • 2020-09-21
      • 1970-01-01
      • 2013-07-02
      • 2012-08-08
      • 2010-09-16
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多