Hiding a dialog without closing it

  • Thread starter Thread starter Michael Wong
  • Start date Start date
M

Michael Wong

Hi,

I am desperate searching for an answer to this problem.
I have shown a dialog (using ShowDialog) in my main form.

As I'm using a virtual desktop, I'm able to switch to another desktop
(which will hide or show the dialog and main form, depending if we are
leaving or coming back to that desktop).

The problem here is that when I leave the dialog open and switch away
and back again, the dialog closes, and the code following ShowDialog is
run, which is not the correct behavior.

This doesn't occur if I use MessageBox.Show

I tried to do a e.Cancel = true in the closing event of the dialog, but
it doesn't work at all.

Any idea?
 
hi Michael,
what kind of virtual desktop are you using?
it sounds like a problem with the desktop switching if it causes an open
Dialog to be closed. does it happen if you stay in the current desktop, and
just switch between applications using Alt-Tab?

tim
 
Hi Tim,

I was desperate waiting for an answer.

The virtual desktop is an open-source one, so I could get the source
code an took a look at the switching process.

VirtuaWin: http://virtuawin.sourceforge.net

Apparently, when switching the desktop, all it does is hiding the main
window, and then all pop-up window (which is the dialog I suppose).

I'll try to hide the dialog manually and see what happens.

If any idea, please, let me know.

Thanks a lot


Michael
 
Hi,
I experirment a similar problem, but not only with popup child window. It
seems that some forms (all deriving from Windows.Forms.form class) interpret
the WS_SHOWWINDOW(false) message as a cancel event. But not all of them.
As i can modify the target forms, i found a wondering workaround, which
consists to shortcut the HIDE message ( 0x0018 whith wparam == 0) in
overriden WndProc of the form, that is to say to return directly.
What a surprise when i see my target form disapear and come back when asked!
I'm searching a rationnal explication... and a way to avoid this not
satisfying workaround.
Thanks


Michael Wong said:
Hi Tim,

I was desperate waiting for an answer.

The virtual desktop is an open-source one, so I could get the source
code an took a look at the switching process.

VirtuaWin: http://virtuawin.sourceforge.net

Apparently, when switching the desktop, all it does is hiding the main
window, and then all pop-up window (which is the dialog I suppose).

I'll try to hide the dialog manually and see what happens.

If any idea, please, let me know.

Thanks a lot


Michael
 
If the dialog is a System.Windows.Forms.Form, and you call ShowDialog() to
display it, setting the DialogResult of the form to anything but "None" will
hide it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

JCM said:
Hi,
I experirment a similar problem, but not only with popup child window. It
seems that some forms (all deriving from Windows.Forms.form class)
interpret
the WS_SHOWWINDOW(false) message as a cancel event. But not all of them.
As i can modify the target forms, i found a wondering workaround, which
consists to shortcut the HIDE message ( 0x0018 whith wparam == 0) in
overriden WndProc of the form, that is to say to return directly.
What a surprise when i see my target form disapear and come back when
asked!
I'm searching a rationnal explication... and a way to avoid this not
satisfying workaround.
Thanks
 
Hi,
Thanks, but not sure it will help. The pb is not to hide the window, but to
not close it while hiding. May be you suggest to put "DialogResult = none" in
constructor so that the further Hide message won't close the window... I'll
check.

After further investigations, i confirm it appends for windows open withs
"ShowDialog" method.
It seems these windows have no parent window... Some have a owner, but it
seems it's not enough. Do you think it is a good reason?
Actually, I'm trying to put a parent window to a form... It is not as easy
as i thunk.
Thanks.
 
Hi JCM,

First, let me assure you that it does not close by simply setting the
DialogResult. I know this from much experience.

Second, the "ShowDialog" method is overloaded. Several overloads allow you
to set the parent window. For example, you can pass "this" to it, if
launching it from a Form, or other windowed Control.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
Hi Kevin,
Thanks for help.
As i can see, the only parameter i can give to ShowDialog is the owner, not
the parent (I have only 2 possibilities... OK i will search again). If i call
showDialog with no parameter, the owner is correctly set also. But I am still
not able to set the correct parent control of my form.

When i use SetParent ( from User32 dll) before ShowDialog, the application
hangs once the form is displayed. Documentation of SetParent tells i have to
refresh the UIState of both impacted window... I don't understand how i can
do this simply.
Thanks
 
Hi JCM,

I'm a little confused. You said you wanted to hide a dialog without closing
it. In the System.Windows.Forms.Form class, you have a few properties that
can relate to another form:

Owner: This is the form that "owns" the form. It is generally used with
dialog boxes, but the business rules are basically that the owned form
minimizes and closes when the Owner does so, and that the owned form cannot
appear behind the owner.
Parent: This is inherited from Control, and indicates the Control which is
the Container for this control. This is not really relevant to this
discussion, unless I'm seriously missing something.
ParentForm: This is the MdiParent of the form, when used in an MDI
interface. It is the Form which contains the form. The containing Form must
have IsMdiContainer set to true to use this.

However, as you can see, only Owner is relevant to a dialog box, which is a
window that pops up *outside* of the Form that spawned it. Only Owner is
really relevant. The Owner gives the dialog box access to the Form that
spawned it, and the Form which spawned it is almost always the form in which
the dialog box is referenced, so it can reference the spawned form via its
instance name.

A Form can also add a form to its "OwnedForms" Collection, by using the
"AddOwnedForm" method. This way, the dialog box is always owned by the
parent Form unless otherwise specified.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
Thanks Kevin,
That way, things are clearer for me. (about Owner/Parent differences)
Anyway, i still have the pb Michael Wong reported at the beginning of that
topic: While hiding a C# modal dialog box, it is destroyed.
I focused on Parent parameter because C++ modal dialog boxes have both owner
& parent window set with the handle of spawning application window. And these
windows are not destroyed while hided...
I'm looking for a way to reproduce in C# the behavior of C++ dialog boxes.

Kenavo
 
The correct way to hide it is to set the DialogResult.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

JCM said:
Thanks Kevin,
That way, things are clearer for me. (about Owner/Parent differences)
Anyway, i still have the pb Michael Wong reported at the beginning of that
topic: While hiding a C# modal dialog box, it is destroyed.
I focused on Parent parameter because C++ modal dialog boxes have both
owner
& parent window set with the handle of spawning application window. And
these
windows are not destroyed while hided...
I'm looking for a way to reproduce in C# the behavior of C++ dialog
boxes.

Kenavo
 
Back
Top