tags:

views:

1764

answers:

4

Hello,

I heard that if I call form.ShowDialog() without specifying the owner, then there can be a case when I will not see the dialog form on screen (it will be hidden with other windows). Is it true? I used ShowDialog() without specifying the owner hundreds of times and I never had any problems with that.

Can you please explain in which situation I could get the described problem?

UPDATE:

Well, I did many experiments and I couldn't get any real unexpected problems with using ShowDialog() (without specifying the owner).

So I think it's just rumors that ShowDialog() can lead to problems. If you don't agree - give me a code sample please that leads to a problem.

+2  A: 

The parameterless ShowDialog() simply uses a "default" parent. For what it's worth, the default parent is whatever the "currently active window" is. When you care what the parent is, you need to set it explicitly.

DaDa
Thank you, but I know that ShowDialog() uses a currently active window, I just don't know how there can be a situation that dialog will appear the way the user will not be able to see it.
nightcoder
A: 

I think that's only a rumour based on one of those "Everything is right it has to work" moments, in which you figure out the actual reason only after quite a while.

There's only one thing that, if you move the dialog form out of visible screen area after showing it, the use won't be able to click on it, and he won't be able to interact with the parent form neither. But you have to explicitly move it out of the visible screen area.

Gorkem Pacaci
This isn't accurate. The owner window can affect multiple aspects of a dialog including, e.g., positioning and the modal parent. ShowDialog creates a modal dialog. If you have a window and don't use it, you can wreak havoc. See the last paragraph of http://blogs.msdn.com/oldnewthing/archive/2004/02/24/79212.aspx
Greg D
+2  A: 

"Currently active Window" usually refers to the foreground window, but only if it belongs to the current thread - see GetActiveWindow in MSDN.

(The actual information is in the community content, but the commenter is right that there is no "per-thread active window", AFAIK).

So when the user switched to another applications (or threads) window, you end up with some "default window". Even if .NET does some magic here, the modality will be broken: the intended parent window does not get disabled (e.g. you could switch to your main window, and close it, or modify something, which often breaks your application due to reentrancy).

Also, if another application is currently active, your dialog will not be shown on top, but it will be hidden behind some other window.

As a minor annoyance, the initial position is usually incorrect or misleading.

In practive, this happens rarely, though: if you open the dialog in response to a menu or button click on your main window, the user doesn't will virtually never manage to switch to another window.

However, it is technically possible, and quite likely to happen if you open the dialog in response to some automation, external message etc.

peterchen
As a general rule, if it's possible to specify the parent, the parent should be specified. It never hurts, and it frequently helps.
Greg D
I actually have this problem - I am running an external process from an outlook plugin - and it doesn't show the dialog. Any ideas how to show the dialog ? It doesn't have any form -> i just want to show a dialog. If I call "MessageBox.Show" before - it works - otherwise not.
bernhardrusch
@bernhardrusch: Did you try specifying the .NET equivalent of GetDesktopWindow as parent?
peterchen
+2  A: 

One annoyance I found with ShowDialog() Vs ShowDialog(this).

Run the TestApp, show the newform.ShowDialog(), click "show Desktop" on your taskbar or Quick launch toolbar, click on the TestApp on the taskbar. It shows the Mainform. You have to do an Alt-Tab to get to your newform.

VS

Run the TestApp, show the newform.ShowDialog(this), click "show Desktop" on your taskbar or Quick launch toolbar, click on the TestApp on the taskbar. It shows the newform on top.

Sun
I am having the exact the problem you describe. But I have changed ShowDialog() to ShowDialog(this) and it hasn't helped.
Colin