Form1 OpenNewForm = new Form1();
OpenNewForm.Show();
"OpenNewForm" is the name of the Form. In the second the form opens.
If you want to close the previous form:
this.Close();
In a single line it would be:
(new Form2()).Show();
Hope it helps.
//To open the form
Form2 form2 = new Form2();
form2.Show();
// And to close
form2.Close();
Hope this helps
On any click event (or other one):
Form2 frm2 = new Form2();
frm2.Show();
I assume your talking about windows forms:
To display your form use the Show() method:
Form form2 = new Form();
form2.Show();
to close the form use Close():
form2.Close();
If you need to show Form2
as a modal dialog, from within Form1
do:
var form2 = new Form2();
if (form2.ShowDialog() == DialogResult.OK)
{
// process results here
}
A modal dialog will retain focus while it is open; it will set the parent windows (Form1
) "in the background" until it is closed, which is quite a common practice for settings windows.
Start with this:
var dlg = new Form2();
dlg.ShowDialog();
If you want to open Form2
modally (meaning you can't click on Form1 while Form2 is open), you can do this:
using (Form2 f2 = new Form2())
{
f2.ShowDialog(this);
}
If you want to open Form2 non-modally (meaning you can still click on Form1 while Form2 is open), you can create a form-level reference to Form2 like this:
private Form2 _f2;
public void openForm2()
{
_f2 = new Form2();
_f2.Show(this); // the "this" is important, as this will keep Form2 open above
// Form1.
}
public void closeForm2()
{
_f2.Close();
_f2.Dispose();
}
Respectively Form.Show() (or Form.ShowDialog() if you want the second form to be modal), and Form.Hide() (or Form.Close(), depending on what you mean by close it
).
Source: Stackoverflow.com