Srihari
Srihari

Reputation: 2429

How to hide a form while we move to next form in Winforms?

I created few Forms while running this project I need to hide the 1st Form while I call the second Form from first Form button on runtime. I tried this code **Login.ActiveForm.Hide();** but it showing error like Object reference not set to instance of object.

I need to hide that From1 while clicking button on Form1 & move onto second Form2 now If I click button on Form2 I need to show that hidden Form1.

Help me to complete this task.

Thanks in advance. Srihari

Upvotes: 0

Views: 55

Answers (1)

melodiouscode
melodiouscode

Reputation: 2069

When you open the next form (presuming it is not opened as a .ShowDialog()) then you can call this.Hide(); to hide the first form.

In the button click event on form2 you can traverse the OpenForms collection and reshow form1.

An example being:

foreach ( Form openForm in Application.OpenForms ) {
   if ( openForm.GetType ( ) == typeof ( Form1 ) ) {
       openForm.Show();
   }
}

Upvotes: 1

Related Questions