is there way to make the default messagebox ( MessageBox.Show()
) TopMost true which also stays on top even if the messagebox lost the focus?
I don't want to create a custom one.
Is there a way doing it by using SetWindowHook or something similar?
I already researched a lot but all solutions I found were not working.
Can someone help me out how to do this in VB.net or C#?
Asked
Active
Viewed 893 times
-1

nexno
- 429
- 8
- 26
2 Answers
1
The simplest way is to p/invoke the MessageBox
function and pass the MB_SYSTEMMODAL
flag.

David Heffernan
- 601,492
- 42
- 1,072
- 1,490
-
Thats sounds interessting. I will have a look at this now. :^) – nexno Apr 04 '14 at 20:21
-
There is MB_TOPMOST constant: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx – prq Apr 04 '14 at 20:22
-
@prq I've heard reports that `MB_TOPMOST` is ignored on some modern Windows versions. Anyway `MB_SYSTEMMODAL` certainly works. – David Heffernan Apr 04 '14 at 20:23
0
I got it. This will show a messagebox on top of all windows opened, doesn't matter if they have Topmost options or not, the messagebox appears always in front.
Public Enum MessageBoxResult As UInteger
Ok = 1
Cancel
Abort
Retry
Ignore
Yes
No
Close
Help
TryAgain
ContinueOn
Timeout = 32000
End Enum
Public Enum MessageBoxOptions As UInteger
SystemModal = &H1000
NoFocus = &H8000
SetForeground = &H10000
Topmost = &H40000
End Enum
<DllImport("user32.dll", EntryPoint:="MessageBoxW", SetLastError:=True, Charset:=CharSet.Unicode)> _
Public Shared Function MessageBox(hwnd As IntPtr, _
<MarshalAs(UnmanagedType.LPTStr)> lpText As String, _
<MarshalAs(UnmanagedType.LPTStr)> lpCaption As String, _
<MarshalAs(UnmanagedType.U4)> uType As MessageBoxOptions) As <MarshalAs(UnmanagedType.U4)> MessageBoxResult
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox(IntPtr.Zero, TextBox2.Text, TextBox1.Text, MessageBoxButtons.OK Or MessageBoxOptions.SystemModal + MessageBoxOptions.Topmost + MessageBoxOptions.SetForeground + MessageBoxIcon.Information)
End Sub
End Class

nexno
- 429
- 8
- 26
-
This is what I told you right? Also, don't be so sure that another topmost window cannot appear on top of yours. – David Heffernan Apr 04 '14 at 21:33
-
Well I opened the messagebox with a button click from my form. For testing I've Set the the form to topmost aswell. When i open the messagebox it appears in front of the topmostform. As soon as I click on the form again the form gets in forground and the msgbox gets in the background. Its not always on top, only on its execution. – nexno Apr 04 '14 at 21:50
-
-
Yes, I do know why that occurs. However, you seem to want to answer the question yourself. I'm not really encouraged to do more in the circumstances. I'd prefer us to deal with the original question before moving on to this new question. – David Heffernan Apr 04 '14 at 21:55
-
I dont think it is necessary to create a new question for this so I modified it ;) I am really interessted why that is. Do you mind telling me?:) – nexno Apr 04 '14 at 22:12
-
Yes I do mind actually. I think I answered the question. I don't understand why you repeated my answer here. I do understand why your topmost window is not staying on top. But I don't think that was the original question. – David Heffernan Apr 04 '14 at 22:17