karthik448

Hi,

I have two forms and I'm loading one of the forms as a default. What I want to do is, in some event of the first form(parent), I want to open the second form(child) and close the parent form. I thought I could use the following code,

Form1 parent = new Form1();

parent.Show();

this.Close();

Closing the parent will close the child also. Any idea on how I go about this.

Thanx for any help.



Re: Windows Forms General Form troubles-open child from parent form..

TilakGopi

Hi Karthik,

Closing the parent will close the child also. ---

Yes, closing the parent will definitely close the child.I tried it.It happened.

But ,it seems the code u wrote in the post is in reverse.First u instanatiate Parent,on one of it's events,u open the child form and in one of the events of child ,u close the parent .Is this the flow

I followed this flow, and observed that closing the parent form is closing the child also.

For further issues on this,feel free to post again.

Thanx,

Ch.T.Gopi Kumar.






Re: Windows Forms General Form troubles-open child from parent form..

karthik448

I have found a solution to this.

I have a context class(static class) which acts as a global variable repository. In this class, I have a Form object in which I store the parent form's current instance. For ex,

Global repository,

static class context

{

public static Form formObj;

}

In the particular event of the parent form in which u want to show the child, do the following.

context.formObj = this;

this.Hide();

Form1 form1 = new Form1(); // form1 is my child form.

form1.Show();

Now I can access this global form object from the child form and subsequently close it also. for example,

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

context.formObj.Close();

}

And now my troubles are over...





Re: Windows Forms General Form troubles-open child from parent form..

TilakGopi

Hi Karthik,

Good solution!,

But i wanna let u know one more.., u tell me ,which is better,after understanding my code.

Have a private variable of parent form variable in child form.Modify child form's constructor or have set property for this variable.Set it either by child class constructor or property,when u instantiate the child class object.Now u can use this parent form variable , how ever u want.

It is illustrated as follows

=========================================================

[Code language = C#]

class ParentForm:Form

{

...

};

class ChildForm:Form

{

private ParentForm frmParent;

public ChildForm(ParentForm refParentForm)

{

frmParent = refParentForm;

}

.....

}

Now u can modify the ur code as follows

Form1 form1 = new Form1(this); // form1 is my child form.

form1.Show();

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

frmParent.Close();

}

[/Code]

============================================================

Thanx,

Ch.T.Gopi Kumar.






Re: Windows Forms General Form troubles-open child from parent form..

karthik448

Hi,

I am new to programming and hence I don't know which method is better or worse. I used the global repository method because I have many forms in my project which need access to a number of common variables and controls.

Anyways..Thanx for providing me with more options, Always appreciate it.