[c#] 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.

This question is related to c# winforms

The answer is


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

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

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

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.


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

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

Search some samples on the Internet.


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

Game_Menu is the form name

Form1 is the object name