3

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.

6
  • is this for winforms? Commented Dec 9, 2017 at 19:56
  • yes it is windows form Commented Dec 9, 2017 at 19:57
  • 1
    Possible duplicate of Dynamically add multiple buttons to wpf window? Commented Dec 9, 2017 at 19:57
  • 1
    @YahyaHussein this question targets Winforms not WPF. Commented Dec 9, 2017 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?... Commented Dec 10, 2017 at 1:45

3 Answers 3

15

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);
}
Sign up to request clarification or add additional context in comments.

8 Comments

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); }
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.
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
this answer did'nt help me. it keep saying "namespace button not found" can you show us the results in cmd?
@gleacc Button isn't a namespace, it's a class. Also, what do you mean cmd? This is a C# example.
|
2

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

    }

Comments

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

 }

4 Comments

why? @KurtCamilleri .what was the problem?
@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.
@Adriani6 Objects don't go away by adding them to another collection. you are so naive.
@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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.