Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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

like image 513
subbu Avatar asked Oct 12 '09 07:10

subbu


People also ask

How do I display a form in C#?

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.


5 Answers

Try this:

private void Button1_Click(Object sender, EventArgs e ) 
{
   var myForm = new Form1();
   myForm.Show();
}
like image 113
Johnno Nolan Avatar answered Oct 19 '22 19:10

Johnno Nolan


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

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

Search some samples on the Internet.

like image 10
Nelson Reis Avatar answered Oct 19 '22 19:10

Nelson Reis


private void ButtonClick(object sender, System.EventArgs e)
{
    MyForm form = new MyForm();
    form.Show(); // or form.ShowDialog(this);
}
like image 9
Bryan Avatar answered Oct 19 '22 19:10

Bryan


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();
like image 1
fIwJlxSzApHEZIl Avatar answered Oct 19 '22 17:10

fIwJlxSzApHEZIl


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);
 }
like image 1
Convicted Vapour Avatar answered Oct 19 '22 17:10

Convicted Vapour