Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
and my question is when ever we click on button previous form 2 be closed and new form2 should be open how to write code in windows forms
Posted

1 solution

Try this:
C#
Form2 currentActiveForm2 = null;
void yourButton_Click(object sender, EventArgs e)
{
    if (currentActiveForm2 != null)
    {
        currentActiveForm2.Close();
        // if currentActiveForm2 is null, your button has not been clicked yet, so the form doesn't exist yet and we cannot close it.
    }
    currentActiveForm2 = new Form2();
    currentActiveForm2.Show();
}
 
Share this answer
 
Comments
Parazival 16-Feb-15 7:39am    
thanks its working
Thomas Daniels 16-Feb-15 7:43am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900