Question

I have an MDI application. When I show a message box using MessageBox.Show(), the entire application disappears behind all of my open windows when I dismiss the message box.

The code is not doing anything special. In fact, here is the line that invokes the message box from within an MDI Child form:

MessageBox.Show(String.Format("{0} saved successfully.", Me.BusinessUnitTypeName), "Save Successful", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

Me.BusinessUnitTypeName() is a read only property getter that returns a string, depending upon the value of a member variable. There are no side effects in this property.

Any ideas?

Was it helpful?

Solution

Remove the last parameter, MessageBoxOptions.DefaultDesktopOnly.

From MSDN:

DefaultDesktopOnly will cause the application that raised the MessageBox to lose focus. The MessageBox that is displayed will not use visual styles. For more information, see Rendering Controls with Visual Styles.

The last parameter allows communication of a background Windows Service with the active desktop through means of csrss.exe! See Bart de Smet's blog post for details.

OTHER TIPS

Remove the MessageBoxOptions.DefaultDesktopOnly parameter and it will work correctly.

DefaultDesktopOnly specifies that "The message box is displayed on the active desktop" which causes the focus loss.

These answers are correct, but I wanted to add another point. I came across this question while working with someone else's code. A simple message box was causing the front most window to move to the back:

MessageBox.Show("Hello").

Turns out, there was a BindingSource.Endedit command before the MessageBox. The BindingSource wasn't connected to any controls yet, but it caused the window to change z-positions.

I am only including this note since my search brought me to this question and I thought it might be helpful to someone else.

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