Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top