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:
- the window is a splash screen and should not tightly coupled with your business logic
- the window is created by another class or library than the current one
- the window is out of your control, i.e. from a third party (native) library
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).