Solved

Child To Parent Form Problem

Posted on 2010-11-22
7
793 Views
Last Modified: 2013-12-17
Hey guys,
I have the following problem.
For example there is a main form and it is a parent to all others. It creates a child form and hides. I want the main form to show when the child is closed. I've done this with creatin FormClose event of the child but in the parent form and it calls a function in the parent form, which shows it. So far so good.
The problem is:
When the main form creates a child 1 and the child 1 creates its own child 2 and sometimes child 1 closes this there is no way to show the main form. The other thing is, that somtimes child 2 can be a irect child of the main form, so I can't know who will be creating it.
0
Comment
Question by:IncognitoMan
7 Comments
 
LVL 11

Accepted Solution

by:
SAMIR BHOGAYTA earned 168 total points
ID: 34186170
Hello,

use this code.

Form[] charr = this.MdiChildren;
foreach (Form chform in charr)
chform.Close();
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 166 total points
ID: 34186434
Change the pattern a bit
instead on handling Close of each child form on the main form (or on the form which is the parent of this child) let all child forms have a property which will hold the absolut parent i.e. the main form

public Form MainForm { get; set; }

Now each child will also implement its FormClosing event(the child itself will have the FormClosing event handler) and on FormClosing irrespective of whether it is a direct child of MainForm or a child of the child window of mainForm it will try to show the mainForm if the property above holds a valid reference, so the code in FormClosing of each child would be

if (MainForm != null)
       MainForm.Show();

So if the parent passed a valid reference while creating it then it intended the child to show the parent if the parent does not want the child to show the parent it will let the property be null.

Now lets validate this pattern against the scenarios you mentioned...
Case 1: When the main form creates a child 1 and the child 1 creates its own child 2 and sometimes child 1 closes this there is no way to show the main form.
With our logic:
mainForm creates Child1 and mainForm Hides itself and it intends the Child1 or its siblings to show the mainForm at some stage so it passes its own reference to the MainForm property of the Child window.
Now the Child1 if closed directly without creating another child Child2 then the code in its FormClosing fires and the mainForm is shown. But if this Child1 creates Child2 and closes itself while creating Child2 it will pass the reference it holds to the mainForm to this Child2 and set its own property to null. This way the mainForm reference is now held by Child2 which when closed will have the FormClosing fired and will show the mainForm. this way you can nest as many levels as you want.

Case2: Child2 is directly created by MainForm
With our logic it hardly matters, since Child2 will again hold the reference of mainForm.

Another good thing with this is that in case you have Child1 opening Child2 and you want that on close of Child2 Child1 should be shown then you can simple let the MainForm property of Child2 hold reference to Child1 and not the main form.

I hope I was clear enough...in case you need I can put in a sample code....
0
 
LVL 5

Assisted Solution

by:scottlafoy
scottlafoy earned 166 total points
ID: 34214031
you could create an interface like IParent and put a method on it, implement it on the parent form and fill the method with something like this.show() (not sure off the top of my head what the syntax is for showing a hidden form) Each child that can be created have it take an IParent (or what ever your interface is called) in the constructor and pass the main form into the constructor which implements the IParent interface. Now any subsequent forms that the child opens pass it the same IParent and use the reference to IParent to call the method on the parent form to show itself from the form closing events of the children. You could also modify the code from samirbhogayta: and check for and show any other child forms and if none are available then show itself. If you need more detailed instructions on using an interface like this I could walk you through it.
0
 
LVL 5

Expert Comment

by:scottlafoy
ID: 34421900
Any of three ways would work. Interfaces lend them self perfectly to this situation. Split the points as the responses here all have merrit.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 34505804
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now