Open an existing form from the main form

c#, winforms

Solution

You are creating instance of Form class not the Form2 which you have in your project. Create instance of Form2 which you created earlier and then call ShowDialog in it.

You might have notice the in the program.cs something like Application.Run(new Form1()); Here we create the instance of Form1 and pass to Run method.

Do it this way by creating instance of Form2 and calling ShowDialog() method to show it

Form2 form2= new Form2();
form2.ShowDialog();

Problem

I designed two forms: `Form1` and `Form2`. `Form1` is the main form. There is a button in `Form1`, if I click the button, then `Form2` will pop out. I want to do something on `Form2`. ``` // click button in Form1. private void button1_Click(object sender, EventArgs e) { Form form2= new Form(); form2.ShowDialog(); } ``` But `Form2` is a new form rather than an existing form. It is wrong. How? Thanks.

Original source