469,085 Members | 1,010 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,085 developers. It's quick & easy.

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

.... 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.
Nov 16 '05 #1
5 8703
Elp
Viper wrote:
... 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.
Nov 16 '05 #2
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:
Nov 16 '05 #3
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.

"Viper" wrote:
.... 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.

Nov 16 '05 #4
"Chris Ballard" <Ch**********@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
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
Nov 16 '05 #5
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.

"Viper" wrote:
"Chris Ballard" <Ch**********@discussions.microsoft.com> wrote in message
news:01**********************************@microsof t.com...
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

Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Martin Douglas | last post: by
3 posts views Thread by Isabel | last post: by
4 posts views Thread by Maarten | last post: by
3 posts views Thread by =?Utf-8?B?RGF2ZVA=?= | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.