Click here to Skip to main content
15,895,283 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


thanksss
Posted
Updated 15-Feb-15 18:08pm
v2

1 solution

Add the following code for the button click event You can also use this.Close to close the form if you don't wish to use it later.
C#
private void button1_Click(object sender, EventArgs e)
        {
            Form2 objForm2 = new Form2();
            objForm2.Show();
            this.Hide();
        }
 
Share this answer
 
Comments
Parazival 16-Feb-15 0:38am    
it,s hiding form1
but if i open one more time form1 and click on button it will show another form(form2) and previous form2 also available
Pankaj Bhandari08 16-Feb-15 1:09am    
so you want like only one instance of form2 should be open?
Parazival 16-Feb-15 1:22am    
yes
Pankaj Bhandari08 16-Feb-15 1:36am    
I am assuming that you have some MDI from where you are opening the form1,
Create object of form2 on the MDI.

Form2 objForm2 = null;

now on the button click of the form1, first check for if objForm2 is null or not .

if(objForm2 == null)
{
//Create the form2 object and show it.
Form2 objForm2 = new Form2();
}
if(!objForm2.Visible)
{
//we have the form but visiblity is hidden
objForm2.Visible = true
}

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