2

In the Form1_Load method what code should I write to create a simple button?

 private void Form1_Load(object sender, System.EventArgs e)
 {

 }

So that on Load the button would show.

domo
  • 27
  • 7
Kurt Camilleri
  • 133
  • 1
  • 1
  • 11
  • is this for winforms? – Daniel A. White Dec 09 '17 at 19:56
  • yes it is windows form – Kurt Camilleri Dec 09 '17 at 19:57
  • 1
    Possible duplicate of [Dynamically add multiple buttons to wpf window?](https://stackoverflow.com/questions/5929710/dynamically-add-multiple-buttons-to-wpf-window) – Yahya Hussein Dec 09 '17 at 19:57
  • 1
    @YahyaHussein this question targets Winforms not WPF. – Martin Backasch Dec 09 '17 at 20:15
  • Based on your comments that it simply doesn't work, we can only guess that either you don't have the Load() event properly wired up causing those lines to never be run, or you have other controls on the form obscuring the button, or possibly you are displaying the wrong form somehow?... – Idle_Mind Dec 10 '17 at 01:45
  • has mad syntax errors ![Syntax smh](https://cdn.glitch.global/cb49a521-55df-4886-bace-6fbfee9ea9da/Screenshot%202022-06-18%205.02.38%20PM.png?v=1655586308694) – RagDev Jun 18 '22 at 21:05

3 Answers3

12

As you said it is Winforms, you can do the following...

First create a new Button object.

Button newButton = new Button();

Then add it to the form inside that function using:

this.Controls.Add(newButton);

Extra properties you can set...

newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);

Your issue you're running to is that you're trying to set it on Form_Load event, at that stage the form does not exist yet and your buttons are overwritten. You need a delegate for the Shown or Activated events in order to show the button.

For example inside your Form1 constructor,

public Form1()
{
    InitializeComponent();
    this.Shown += CreateButtonDelegate;
}

Your actual delegate is where you create your button and add it to the form, something like this will work.

private void CreateButtonDelegate(object sender, EventArgs e)
{
    Button newButton= new Button();
    this.Controls.Add(newButton);
    newButton.Text = "Created Button";
    newButton.Location = new Point(70,70);
    newButton.Size = new Size(50, 100);
    newButton.Location = new Point(20, 50);
}
Adrian
  • 8,271
  • 2
  • 26
  • 43
  • namespace SimplePiano { public partial class Form1 : Form { Button nb = new Button(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, System.EventArgs e) { this.Controls.Add(nb); } – Kurt Camilleri Dec 09 '17 at 21:14
  • This is not showing the button when I run the code. I created the button object outside of the method, then in the Load() method I did the this.controls.add(newButton) but it didn't work. – Kurt Camilleri Dec 09 '17 at 21:16
  • Still the same problem. I would try to create a new solution just in case there is a problem with the one I am working with at the moment. Thanks for your help – Kurt Camilleri Dec 09 '17 at 22:09
  • this answer did'nt help me. it keep saying "namespace button not found" can you show us the results in cmd? – gleacc Nov 16 '22 at 21:44
  • @gleacc Button isn't a namespace, it's a class. Also, what do you mean cmd? This is a C# example. – Adrian Nov 16 '22 at 22:29
  • you didnt know that the output was run in a command prompt? – gleacc Nov 17 '22 at 23:48
  • i ment console . – gleacc Nov 17 '22 at 23:49
  • If you're using a command prompt, why would you need a button? It makes no sense. CLI and GUI are two different things. I recommend you ask a separate question with more detail. This example was for WinForms not any form of CLI as that was the scope of this question. – Adrian Nov 18 '22 at 10:01
1

on your eventload form put this code

 private void Form1_Load(object sender, EventArgs e)
    {
        Button testbutton = new Button();
        testbutton.Text = "button1";
        testbutton.Location = new Point(70, 70);
        testbutton.Size = new Size(100, 100);
        testbutton.Visible = true;
        testbutton.BringToFront();
        this.Controls.Add(testbutton);

    }
chopperfield
  • 559
  • 1
  • 7
  • 25
-1

It's simple :

private void Form1_Load(object sender, System.EventArgs e)
 {
     Button btn1 = new Button();
     this.Controls.add(btn1);
     btn1.Top=100;
     btn1.Left=100;
     btn1.Text="My Button";

 }
nAviD
  • 2,784
  • 1
  • 33
  • 54
  • why? @KurtCamilleri .what was the problem? – nAviD Dec 11 '17 at 17:29
  • @nAviD I have you -2 and 2 days later you still don't notice your mistake. Any properties you modify post assigning the button won't take affect. You've already sent this object away, Your `Top` and `Left` and `Text` have no affect on the button. Correct me if I'm wrong. Kurt, you are doing something wrong, there is no way my code does not work for you, I have tested it with an empty WinForms project. – Adrian Dec 12 '17 at 10:47
  • @Adriani6 Objects don't go away by adding them to another collection. you are so naive. – nAviD Dec 12 '17 at 13:40
  • @nAviD As I said "Correct me if I'm wrong". I do stand corrected, I tested that and it does indeed work. That was not a reason to go and give negative points to my answer. In fact that's pretty childish to do so in a 'professional' environment. Anyway, when you edit your post I'll remove my -1 gave it unfairly I admit. You've got a typo. `.add()` should be `.Add()` – Adrian Dec 12 '17 at 14:00