Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.Net MessageBox.Show() moves my form to the back

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?

like image 859
Pittsburgh DBA Avatar asked Nov 18 '25 21:11

Pittsburgh DBA


2 Answers

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.

like image 148
Mitch Wheat Avatar answered Nov 20 '25 13:11

Mitch Wheat


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.

like image 41
DaveK Avatar answered Nov 20 '25 15:11

DaveK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!