65

How to keep a Messagebox.show() on top of other application using c# ??

Anuya
  • 8,082
  • 49
  • 137
  • 222
  • 1
    No need for extra Code, try this: http://stackoverflow.com/questions/4834819/c-sharp-messagebox-to-front-when-app-is-minimized-to-tray –  Jul 23 '13 at 11:56

6 Answers6

117

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.

shox
  • 677
  • 1
  • 5
  • 19
MikeDub
  • 5,143
  • 3
  • 27
  • 44
  • 2
    Yes, I found this one works too! There are several suggestions but I agree that this is the easiest one to implement. – shivesh suman Jun 02 '15 at 05:13
  • Exactly what I needed! – Homer1982 Aug 09 '16 at 16:35
  • Works like charm! This should be accepted as a solution. – Alper Jan 28 '20 at 07:57
  • While this works, I ended up using Xceed MessageBox as that also centers box on top of Application. – Damian Dixon Mar 26 '21 at 09:37
  • Using Poweshell, this is also an accepted approach as well. `[System.Windows.MessageBox]::Show($Message, "Prompt", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning, [System.Windows.MessageBoxResult]::OK, [System.Windows.MessageBoxOptions]::DefaultDesktopOnly )` – Melsy Oct 21 '22 at 14:48
37

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.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
donutboy
  • 606
  • 6
  • 10
19

Another easy way to handle this:

MessageBox.Show(new Form { TopMost = true }, "This is TopMost", "TopMost", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Dave
  • 897
  • 8
  • 8
  • 1
    This is a better solution since it works properly on a multi-screens system. – amirfg Jun 20 '19 at 13:07
  • I used this for about three years in a production application and it works, however I ended up changing it Mike Dub's answer as we now have multiple screens and specifically want the message to always come up on the primary screen. We'll see how it goes. – Cobysan Sep 11 '22 at 15:15
2

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;
        }

    }

}

2

Use the option

MessageBoxOptions.DefaultDesktopOnly

Mario Favere
  • 490
  • 4
  • 9
0

Based on Dave's answer:

WPF:

MessageBox.Show(new Window { Topmost = true }, "Message", "Title");

Windows Form:

MessageBox.Show(new Form { TopMost = true }, "Message", "Title");
datchung
  • 3,778
  • 1
  • 28
  • 29