views:

616

answers:

2

I am showing Dialogs with Form.ShowDialog(). Forms are set to TopMost. Some image processing calculations are performed asynchronly in the background. Their results are rendered in the main form by Invoke. So far so good. Strange thing is that every now and then the modal dialog is moved behind the main form. I guess that happens when the messages that build up the dialog are somehow "disturbed" by Invoke.

Any ideas how to solve this problem?

A: 

Maybe you could try passing the owning form as a parameter to ShowDialog, like that (VB syntax):

Form.ShowDialog(Me)

Not sure if it would change anything, but worth a try...

Also, when you say "Forms are set to TopMost", do you mean the main form, or the modal forms? Do you really need to set it to TopMost? Sometimes TopMost forms have weird behavior...

Meta-Knight
+1  A: 

As a general rule, avoid TopMost unless you absolutely must not. If you absolutely must not, never have more than one TopMost form at a time. (After all, there can't be three TopMost forms--- somebody's got to lose).

First, verify that you're correctly setting the owner when you call Form.ShowDialog(). This will make the new form more likely to appear in an appropriate location and all-around improve behavior of the application. Second, verify that you only have one TopMost window at any time. A combined failure to do these things could most definitely cause your problem. E.g.:

// MainForm.cs -- Don't do this
this.TopMost = true;
childForm.TopMost = true;
childForm.ShowDialog(/*no parent spec'd*/);

Now what happens? Both the main form and the child form are topmost, so one of them's got to lose. MainForm was active when it created childForm as topmost, but childForm doesn't have a parent. Maybe Windows decided to make the desktop its parent. This could result in the main form staying on the of the newly opened child form.

Greg D
ShowDialog(parentHandle) solved my issues as far as I can tell now. But another question arises: Why is there a ShowDialog(/*no parent spec'd*/); that works only in 99 out of 100 cause? This kind of bug can really drive one nuts. I never expected that coding UI will bring the coder into the field of statistics. :)
Matze
Good question, and I don't _know_ the answer. Best guess is related to "http://preview.tinyurl.com/c6cfze". The underlying APIs allow a NULL parent window for whatever reasons (maybe ShowDialog() can't have a parent in some contexts?), so the .Net equivalent does too.
Greg D