Javed Akram
Javed Akram

Reputation: 15344

get back hidden FORM from another FORM

I have two forms Form1 and Form2

I am opening Form2 from Form1 on button_Click

Form2 obj2 = new Form2();
this.Visible = false;
obj2.Show();

then I want to get back Form1 Visible (on disposing Form2) in same states of Controls on which I left.....

Upvotes: 10

Views: 19519

Answers (2)

Shadow Wizard
Shadow Wizard

Reputation: 66389

Oded's answer will work perfectly well, another option with the same result will be to expose public event in Form2 called for example "AfterClose", invoke it when Form2 is disposing and have Form1 add event handler where it show itself. Let me know if you're interested and I'll give some sample code.

Upvotes: 0

Oded
Oded

Reputation: 498904

Your Form2 doesn't know anything about Form1. It will need a reference to it (you can do that by adding a Form type property on Form2 and assign Form1 to it after construction):

//In Form2
public Form RefToForm1 { get; set;}

//In Form1
Form2 obj2 = new Form2();
obj2.RefToForm1 = this;
this.Visible = false;
obj2.Show();

//In Form2, where you need to show Form1:
this.RefToForm1.Show();

Upvotes: 23

Related Questions