Reputation: 1625
I've got three forms, Form1, Form2 ,Form3. A button in Form1 can open Form3 and a button in Form2 can also open Form3. When either button is pressed the respective form is hidden and Form3 is opened. When Form3 is closed it should open the form that has been hidden.
How would I go about doing this?
Upvotes: 0
Views: 71
Reputation: 9322
Another way is to use Acivated and FormClosed events.
Let's say in Form1 you Click
a Button to show Form2
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Activated += new EventHandler(frm2_Activated);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
frm2.Show();
}
Now, this one is when the Form2 showed or is Activated you hide the calling form, in this case the Form1
private void frm2_Activated(object sender, EventArgs e)
{
this.Hide(); // Hides Form1 but it is till in Memory
}
This one when the Called form is Closed in this case Form2 it will then show the hidden Form1 again.
private void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
this.Show(); // Show hidden Form1
}
Upvotes: 0
Reputation: 17600
Form.Show
method can take OwnerForm
as argument so call it like that:
var frm = new Form3();
frm.Show(this);
you can access parent in Form3 by property Owner
so in closing event:
private void FormIsClosing(object sender, FormClosingEventArgs e)
{
var owner = this.Owner;
if (owner != null)
{
owner.Show();
}
}
Upvotes: 3