You have to use the Hide or Close method of the form on the actual instance of the form, not on a new one:
private Form2 form2;
private void OpenForm2()
{
if (form2 == null)
{
form2 = new Form2();
}
form2.Show();
}
private void CloseForm2()
{
if (form2 != null)
{
form2.Close();
form2 = null;
}
}
"Its not working. I have button on form 1, I want to use the same button to show and hide the form 2."
Not exactly a big change:
private Form2 form2;
private void ToggleForm2()
{
if (form2 == null)
{
form2 = new Form2();
form2.Show();
}
else
{
form2.Close();
form2 = null;
}
}