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 n开发者_如何学Goew Form on click of a button.
Try this:
private void Button1_Click(Object sender, EventArgs e )
{
var myForm = new Form1();
myForm.Show();
}
Double click the button in the form designer and write the code:
var form2 = new Form2();
form2.Show();
Search some samples on the Internet.
private void ButtonClick(object sender, System.EventArgs e)
{
MyForm form = new MyForm();
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();
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);
}
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.
Game_Menu Form1 = new Game_Menu();
Form1.ShowDialog();
Game_Menu is the form name
Form1 is the object name
精彩评论