views:

3722

answers:

5

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?

+1  A: 

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.

DaveK
Thank you very much. This answer was functionally similar to Mitch Wheat's and came in at the same time. I gave his the nod because of the MSDN snippet/reference.
Pittsburgh DBA
+4  A: 

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.

Mitch Wheat
Thank you very much. This was frustrating.
Pittsburgh DBA
Thanks also for the blog link. Very good research and good information to file away for further use!
Pittsburgh DBA
A: 

Nice work Mitch - pulling the last of my hair out with this one!

A: 

Thanks a lot...This solution solved my problem after 2 hours of struggling...

A: 

THANK YOU................................................