How to keep a Messagebox.show() on top of other application using c# ??
I tried the solution provided by donutboy and it doesn't seem to accept 0x40000 (or 40000) as a valid option as a MessageBoxOptions Enum value.
However I have found that using MessageBoxOptions.DefaultDesktopOnly has the same effect and keeps the MessageBox on top until it is confirmed by the user. ie.
MessageBox.Show("Hello there", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
This is likely the simplest native solution on offer.
[System.Windows.MessageBox]::Show($Message, "Prompt", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning, [System.Windows.MessageBoxResult]::OK, [System.Windows.MessageBoxOptions]::DefaultDesktopOnly )
–
Ridgeway There's a better solution, without creating a new form.
MessageBox.Show("Message Text", "Header", MessageBoxButtons.OK, MessageBoxIcon.None,
MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000); // MB_TOPMOST
The 0x40000 is the "MB_TOPMOST"-Flag.
Another easy way to handle this:
MessageBox.Show(new Form { TopMost = true }, "This is TopMost", "TopMost", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
The problem with using new Form { TopMost = true }
as the first argument is that it fails to properly dispose of the new form when it is done.
It took a lot of work to find this problem (several weeks). The only symptom was that the program would "Fail to Respond" a half hour later. Totally locked up, had to kill it with an attached debugger or the task manager, no debug info available.
To solve this, you need something like this:
using (Form form = new Form {TopMost = true})
{
var retval = MessageBox.Show(form, text, caption, ok, error);
form.Dispose();
return retval;
}
Even better, write your own "MyMessageBox" class, and use that:
public static class MyMessageBox {
public static DialogResult Show(string text, string caption, MessageBoxButtons ok, MessageBoxIcon error)
{
using (Form form = new Form {TopMost = true})
{
var retval = MessageBox.Show(form, text, caption, ok, error);
form.Dispose();
return retval;
}
// return UseForm ? MessageBox.Show(form, text, caption, ok, error) : MessageBox.Show(text, caption, ok, error);
}
public static DialogResult Show(string text, string caption, MessageBoxButtons ok)
{
using (Form form = new Form { TopMost = true })
{
var retval = MessageBox.Show(form, text, caption, ok);
form.Dispose();
return retval;
}
}
public static DialogResult Show( string text, string caption)
{
using (Form form = new Form { TopMost = true })
{
var retval = MessageBox.Show(form, text, caption);
form.Dispose();
return retval;
}
}
public static DialogResult Show(string text)
{
using (Form form = new Form { TopMost = true })
{
var retval = MessageBox.Show(form, text);
form.Dispose();
return retval;
}
}
}
using {...}
there is no need to call Dispose() manually. using
will do that for you. So it could just be return MessageBox.Show(form, ...)
inside the using
block –
Mauve Use the option MessageBoxOptions.DefaultDesktopOnly
.
Based on Dave's answer:
WPF:
MessageBox.Show(new Window { Topmost = true }, "Message", "Title");
Windows Form:
MessageBox.Show(new Form { TopMost = true }, "Message", "Title");
© 2022 - 2024 — McMap. All rights reserved.