Modal forms, Owner property and minimization behaviour

D

dancardoso

Hi everybody,

I believe something weird is happening with my windows forms desktop
application. I have a main form (Form1, shown using Application.Run)
and this main form opens a modal form (Form2), using ShowDialog(this)
so that the owner of Form2 is Form1. Form2 does the same thing with
Form3 (ie, Form2 opens Form3 modally, using showDialog(this.Owner), so
that the owner of Form3 is Form1 too. Form3, once more, opens Form4
passing a reference to Form1 in the owner parameter of showDialog.

The weird thing happens now: Form4 has a button that minimizes the
application when clicked by setting Form1.WindowsState to Minimized.
When Form1 is minimized, all its 3 owned forms are minimized too. 'til
this point, everything's fine, but when Form1 is restored, only Form1
itself appear and I can freelly use it. The other modal windows do not
come back with Form1. Indeed, after some debugging I realized they were
being closed just after form1's minimization. Is this really the
expected behaviour?

What I need to do is to minimize the application and when the user
restores it, all 4 forms are shown as before minimizing. Is there any
way I can accomplish this? The reason for that is that in the real
application, the last form has a functionality to start another process
and I want the hole app to minimize so that the user can work with the
just-started application. After finishing work, the user should be able
to go back and restore the first application to continue working.

Thanks and sorry for the long post.

Daniel.
 
D

dancardoso

Just in case anyone goes through the same situation, I just realized
that if I minimize the child form (i.e. Form4 in the example), all the
owner forms are minimized too. That's the behaviour I want. I just
still can't understand the logic behind this because I imagined that
the childs should minimize with the parent, not the opposite.
 
A

AnsweringMachine

I digged for a while and it turned out that even if I explicitly kept
references (as opposed to relying on the references in the main form's
OwnedForms collection) to all these form objects, the owned modal
dialog boxes still got closed and their references removed from the
OwnedForms collection after the main form got minimized and restored.
And the CloseReason on the closed forms were None, the description of
which is "The cause of the closure was not defined or could not be
determined."

I guess the logic behind this is that modal dialog boxes are supposed
to be 'modal', meaning they are supposed to remain on the screen for a
short while and get dismissed after taking input from the user. If you
want something that moves and changes with the owner form, you might
have to use modeless dialog boxes.
 
K

kaisatsu

I've encountered the same problem and also realized that in this case,
the modal
Form's OnClosing event is being called, but not OnClosed, which will
actually only be called if the DialogResult is set on the modal form.

Unfortunately, I'm still searching for a good solution to keep the
modal form around!

-Meg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top