I recently found out that by default MessageBoxes were not the top most form when displayed by default and I was wondering if anyone knew any circumstances when you wouldn't want the messagebox to be shown on top of other windows?
I found the issue when I started to show splash screens whilst loading an application, and it looked like my program was still running but there was a MessageBox
behind the splash screen that was waiting for input.. The splash screen was shown on a different thread to the thread that called the messagebox so I imagine this is why it didn't appear above the splash; but this still doesn't explain why MessageBox doesn't have the MB_TOPMOST
flag by default?
Edit
To better clarify: in the end I had to do something similar to this in the end to make a messagebox, code isn't exactly correct as wrote from memory)
[DllImport("User32.dll")] private int extern MessageBox(windowhandle, message, caption, flag); public static void MessageBox(windowhandle, string message, string caption) { MessageBox(windowhandle, message,caption, MB_TOPMOST); }
Your best bet would be to create a form that acts like a MessageBox - i.e. set its TopMost property to true and call it with the . ShowDialog() method. MessageBox. Show(new Form() { TopMost = true }, "I'm on top!");
A message box is a modal dialog, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur. Show activity on this post. The way that MessageBox.
The first parameter msg is the string displayed in the dialog box as the message. The second and third parameters are optional and respectively designate the type of buttons and the title displayed in the dialog box. MsgBox Function returns a value indicating which button the user has chosen.
MessageBox with Default Button By default, the first button is the default button. The MessageBoxDefaultButton enumeration is used for this purpose and it has the following three values. The following code snippet creates a MessageBox with a title, buttons, and an icon and sets the second button as a default button.
The proposed solutions work if you can get a handle or reference to the window the dialog is supposed to appear on top of. However, this may not always be possible or easy to achieve:
In such scenarios, you could use the Win232 MessageBox
API from User32.dll
, but a simpler, managed solution is also available:
MessageBox.Show(new Form { TopMost = true }, "Hello, I'm on top!");
The code new Form { TopMost = true }
will create a hidden form with the MB_TOPMOST
property, which is inherited by the messagebox dialog window. As a result, it will appear on top of all your other windows. Using new Form()
inline has no side-effects, has no visual appearance and it will be destroyed normally via the garbage collector.
Note: if you are not inside a form already, don't forget the namespace, this is System.Windows.Forms.MessageBox
, not System.Windows.MessageBox
! (thanks, user1).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With