user641490
user641490

Reputation: 51

c# gui - navigate between forms

I'm programming c# GUI and I have 2 forms.

Form1 is my main form, and it has a button to open form2.

When the button in form1 is being clicked, I hide form1, create a new object of form2 and show form2.

I have a back button in form2. I want the behavior of this button to close form2, and show again the hidden form1.

How can I do it?

Upvotes: 1

Views: 1292

Answers (2)

phadaphunk
phadaphunk

Reputation: 13313

Have you tried

     Form1.Visible = true;
     Form1.Activate();

And then in the Form1 VisibleChanged Event you simply write

     Form2.Close();

Upvotes: 1

Oded
Oded

Reputation: 499062

Have your form1 subscribe to the VisibleChange event of form2 and act accordingly. It will have to "remember" whether form2 is visible or hidden (or query it directly).

The alternatives are:

Your form2 will need a reference to form1.

This can be done in a number of ways - passing it in a constructor parameter, adding a property and assigning form1 to it.

Either of these ways will tightly couple these forms to each other (bad thing).

Upvotes: 1

Related Questions