Reputation: 3299
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
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
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
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
Reputation: 21
Game_Menu Form1 = new Game_Menu();
Form1.ShowDialog();
Game_Menu is the form name
Form1 is the object name
Upvotes: 0
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
Reputation: 29657
Try this:
private void Button1_Click(Object sender, EventArgs e )
{
var myForm = new Form1();
myForm.Show();
}
Upvotes: 71
Reputation:
private void ButtonClick(object sender, System.EventArgs e)
{
MyForm form = new MyForm();
form.Show(); // or form.ShowDialog(this);
}
Upvotes: 10