subbu
subbu

Reputation: 3299

To show a new Form on click of a button in C#

I am new to C# can anybody tell me on How to show a new Form on click of a button.

Upvotes: 35

Views: 218089

Answers (7)

Vi sharma
Vi sharma

Reputation: 15

1.Click Add on your project file new item and add windows form, the default name will be Form2.

2.Create button in form1 (your original first form) and click it. Under that button add the above code i.e:

var form2 = new Form2();
form2.Show();

3.It will work.

Upvotes: 0

Convicted Vapour
Convicted Vapour

Reputation: 11

This worked for me using it in a toolstrip menu:

 private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
 {
     calculator form = new calculator();
     form.Show(); // or form.ShowDialog(this);
 }

Upvotes: 1

Nelson Reis
Nelson Reis

Reputation: 4810

Double click the button in the form designer and write the code:

    var form2 = new Form2();
    form2.Show();

Search some samples on the Internet.

Upvotes: 9

Jalaluddin
Jalaluddin

Reputation: 21

Game_Menu Form1 = new Game_Menu();
Form1.ShowDialog();

Game_Menu is the form name

Form1 is the object name

Upvotes: 0

fIwJlxSzApHEZIl
fIwJlxSzApHEZIl

Reputation: 13320

This is the code that I needed. A defined user control's .show() function doesn't actually show anything. It must first be wrapped into a form like so:

CustomControl customControl = new CustomControl();
Form newForm = new Form();
newForm.Controls.Add(customControl);
newForm.ShowDialog();

Upvotes: 1

Johnno Nolan
Johnno Nolan

Reputation: 29657

Try this:

private void Button1_Click(Object sender, EventArgs e ) 
{
   var myForm = new Form1();
   myForm.Show();
}

Upvotes: 71

user142914
user142914

Reputation:

private void ButtonClick(object sender, System.EventArgs e)
{
    MyForm form = new MyForm();
    form.Show(); // or form.ShowDialog(this);
}

Upvotes: 10

Related Questions