【问题标题】:C# Windows Form (Adding New Controls At Running) [duplicate]C# Windows 窗体(在运行时添加新控件)[重复]
【发布时间】:2018-07-20 21:34:14
【问题描述】:

我已经启动了新的窗体应用程序,我想在按下任何按钮时添加一些控件实例。(如果我按下 AddButton,我希望程序再添加两个按钮)。

【问题讨论】:

  • 在设计器中创建控件并将其可见性设置为 false。然后在按下按钮后,将可见性设置为 true。

标签: c# .net windows-forms-designer


【解决方案1】:

您至少需要一些x,y 坐标来放置它,然后您可以检查按钮的属性并根据需要进行更改。

private void createButton(string name, int x, int y)
{
    // Create button
    Button btn = new Button();

    // Set button name
    btn.Name = name;

    // Set location
    btn.Location = new Point(x, y);

    //Hook our button up to our generic button handler
    btn.Click += new EventHandler(btn_Click);

    // Add it to the main panel
    // panel1 is your application name
    panel1.Controls.Add(
}

void btn_Click(object sender, EventArgs e)
{
    MessageBox.Show("This is the handler of the button that we created");
}

然后在创建按钮的主按钮中,您可以像这样调用按钮:

createButon("some name", 5,5);

【讨论】:

    猜你喜欢
    • 2013-12-20
    • 2014-02-17
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2023-03-10
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多