Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I wan to Hide Parent window when it's child will be shown and when child window will be closed again parent window should again visible.

//parent window calling child form//
ChildForm s=new ChildForm();
s.show();
this.hide();


//buuttton on child forn to close it//

Form p=(Form)this.Parent;
p.show();
this.close();


It's not working how to do that.
Posted
Updated 25-Mar-21 18:59pm

First of all, you should understand that child-form dependency is rendered defunct for forms. You can override it by assigning TopLevel of the form to false, but… don't even try, there is nothing good on doing so; you would simply get one form inside another, which makes no sense.

You code is "not working", just because this.Parent for a form is really null, normally. Also, you should never close a form if you plan on showing again; you better need to hide it (in the FormClosing event, too).

You need to change the design to control the visibility, which is not a problem at all. First of all, I would recommend to use only one form. What are now forms, could be separate panel showing or hiding in the form. If you still need more forms, which I would not recommend, you need to use hiding and showing, not closing. The problem is that the code showing one form and hiding another form should be done in the content where the references to both forms are already known. The forms are pretty much independent; one form is unaware of others, so you need to provide a reference to another form. How to do it, depends on your purpose, but is trivial.

—SA
 
Share this answer
 
Comments
Espen Harlinn 8-Oct-12 18:05pm    
Good points :-D
Sergey Alexandrovich Kryukov 8-Oct-12 18:10pm    
Thank you, Espen.
--SA
This is a really old question, but the solution is so simple, it is worth mentioning

In parent you create the child form, hide the parent, and show the child

yourForm aForm = new yourForm();
aForm.ShowDialog();
this.Show();

In Child Form, you have a button to hide it, and include

this.Hide();

Be sure to use ShowDialog() instead of Show() otherwise the code will execute this.Show() immediately after displaying the child.

With it like this, both the child window, and the parent remain active and you can toggle in between, assuming you created aForm outside of your function to open the Child. Anyways, pretty simple.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900