I am new to C# can anybody tell me on How to show a new Form on click of a button.
Writing C# Code to Display a Modal FormPress F5 once again to build and run the application. After pressing the button in the main form to display the sub form you will find that the main form is inactive as long as the sub form is displayed. Until the sub form is dismissed this will remain the case.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With