简体   繁体   中英

C# - Creating a button when another button is clicked

Maybe I am blind, but I'v looked for an answer at this question quite a while but didn't find it. I know, probably it's pretty easy and basic, but how do you create a button once an aleardy exiting button is pressed and how do I specify what locatin and sizes to have? Thanks alot !

Solution is for winforms

private void button1_Click(object sender, EventArgs e)
{
        Button button = new Button();
        button.Location = new Point(70,70);
        button.Size = new Size(100, 100);

        this.Controls.Add(button);   
}

If you want to create , just create with standard new ; WinForms sample:

// On "MyCreateButton" click
private void MyCreateButton_Click(object sender, EventArgs e) {
  Button newButton = new Button() {
    Parent = this,                // place new button on the current form
    Location = new Point(10, 20), // place at x = 10, y = 20
    Text = "New button",          // Text on the button 
    //TODO: Add whatever button parameters you want 
  };
}

why don't you just create all the buttons that you need and then hide it when run ? (show only button1) Then when button1 is clicked, show/set the button2 to visible = true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM