How to close parent form and not to close a child form also?

  • Thread starter Thread starter Viper
  • Start date Start date
V

Viper

.... say, like MS Word opens another instance of himself when you open
another document.
If you close the first 'Word', the second document remains alive. I can't
figure out how to do the same with

mySecondForm.Show();
myFirstForm.Close(); // also closes mySecondForm too

Thanks.
A lot.
 
Viper said:
... say, like MS Word opens another instance of himself when you open
another document.
If you close the first 'Word', the second document remains alive. I
can't figure out how to do the same with

This is because, as you say, Word opens another instance of himself. I don't
think that there is a Parent/Child relationship between the two Word windows
and this is why closing the first one doesn't cause the second one to close.
mySecondForm.Show();
myFirstForm.Close(); // also closes mySecondForm too

In your case i suggest you to remove the Parent/Child relationship as it
seems to be not appropriate here. Just have 2 top level windows and you'll
be fine.
 
Hi Viper,

If you close a parent form (MDI Parent form, that is), you close all the
child forms with it. In your example of using Word doc, you are closing the
first child instance of the document... not the parent itself! When you
open any word document, it opens the Word MDI (the engine WINWORD.exe) along
with a child document. In the latest version of OSs (XP and 2003), the
interface is shown as if all the documents are SDI when in fact they are
not. Use your task manager and you'll notice there are as many instances of
word documents as you have opened, but if you look in the process tab, you
will notice only one winword.exe. Try killing the winword.exe process and
effectively, you'd kill all the word documents.

In your example, after showing the second form, the control comes back
to first form (which created and holds a reference to the second form). If
you want to have the second form stay alive after the first form is done
with, then make sure you have a reference to the second form outside the
scope of the first form. Like, add a new form variable in a third class.
One implementation could be...

clsShared.cls:
 
Viper,

I assume that you run your application using the first form. i.e. your Main
routine is something like:
[STAThread]
static void Main()
{
Application.Run(new FirstForm());
}

Take a look at Application.Run on MSDN library. The overload used above
allows you to specify the main form, and the application installs a Close
event handler on this form which calls the ExitThread method on the
application to close the app.

As an alternative, you can create the form yourself in the Main() routine,
then show the form modelessly using FirstForm.Show(), and then call
Application.Run(). Then your app will keep running until one of the forms
calls Application.ExitThread(). eg:

[STAThread]
static void Main()
{
FirstForm ff = new FirstForm();
ff.Show();
Application.Run();
}

Dont forget to call Application.ExitThread() anywhere where the last visible
form closes, otherwise your application will stay alive with no UI visible.

HTH,
Chris.
 
Chris Ballard said:
Viper,
...
Dont forget to call Application.ExitThread() anywhere where the last
visible
form closes, otherwise your application will stay alive with no UI
visible.

HTH,
Chris.

Thank you.

How can I detect, probably in OnClosing, that this is the last Form closing?

Robert
 
Robert,

The easiest way will be to keep track of each form that you open yourself.
Perhaps you can design a factory object which is used to create each form and
to close each form. This factory class could then detect when the final form
is closed and call ExitThread for you.

Chris.
 
Back
Top