11

I have some code that popups a message box:

MessageBox.Show(this,
                 "You have not inputted a username or password. Would you like to configure your settings now?",
                 "Settings Needed",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question);

My problem is when this pops up my app is usually minimized to the tray. As a result the messagebox doesn't come to the front and it also doesnt appear along the start bar. The only way of seeing it is by alt-tabbing.

Here is the code that minimizes my app (the parent) to the tray:

if (FormWindowState.Minimized == WindowState)
{                
      Hide();                
}
Garrison Neely
  • 3,238
  • 3
  • 27
  • 39
Luke Belbina
  • 5,708
  • 12
  • 52
  • 75
  • 10
    you have no idea what my application does and whether this is acceptable in the context of the problem so if your not going to be constructive dont comment. – Luke Belbina Jan 29 '11 at 10:45
  • http://stackoverflow.com/a/4834954/206730 is better solution ? – Kiquenet Aug 22 '13 at 08:34

5 Answers5

32

There's an additional flag you can specify as an option to the standard Windows MessageBox function that isn't exposed in the WinForms wrapper.

What you're looking for is called MB_TOPMOST, which ensures that the message box is displayed as the top-most window over everything else on your desktop. Simply amend your code as shown below:

MessageBox.Show(this,
                "You have not inputted a username or password. Would you like to configure your settings now?",
                "Settings Needed",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button1,  // specify "Yes" as the default
                (MessageBoxOptions)0x40000);      // specify MB_TOPMOST
Draken
  • 3,134
  • 13
  • 34
  • 54
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    it still doesnt come to front or top. i am not able to drag that messagebox around or click on the ok/close buttons. – Sakthivel Jun 11 '14 at 12:22
  • @shakthi apparently TopMost doesn't work on vista. maybe this is your problem, see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx – hrh Sep 24 '14 at 17:23
  • 1
    I thought this was the answer I was looking for - but then - vb.net said no. "The value of argument 'options' (262144) is invalid for Enum type 'MessageBoxOptions'" .. any clues? – Peter pete Dec 10 '14 at 09:49
  • This doesn't work in a WPF app. You get an exception that the MessageBoxOptions enum doesn't like the 0x40000 value. – DiscDev Feb 12 '15 at 22:08
  • @Peter pete - you should be able to use this as the parameter in vb.net: `CType(&H40000, MessageBoxOptions)`. Worked a treat for me. – Nick Shaw Dec 08 '15 at 12:35
20

you can try like this

MessageBox.Show(new Form() { TopMost = true }, "You have not inputted a username or password. Would you like to configure your settings now?",
                 "Settings Needed",
                 MessageBoxButtons.YesNo,
                 MessageBoxIcon.Question);
anishMarokey
  • 11,279
  • 2
  • 34
  • 47
  • great! when I close the message box it would then kill that form too, right? (it looks like it does) – Luke Belbina Jan 29 '11 at 03:06
  • 4
    @nextgenneo: There's no reason you should have to create a new form just to display a message box. See my answer for a better solution. (And no, it doesn't necessarily kill the form. It's still running in the background.) – Cody Gray - on strike Jan 29 '11 at 03:26
  • `Form` has `IDisposable` so having `using` around it is recommended. The reason for this being the better solution (and in that case reusing the topmost instance) is so that if you open multiple dialogs they can all get to be the actual visible dialog, and not getting hidden behind each other and being unable to read them. – NiKiZe Sep 24 '16 at 15:58
5

I only needed this for testing, so if you don't mind being extra cheesy, MessageBoxOptions.ServiceNotification will do the trick...

        MessageBox.Show(message,
            "Error",
            MessageBoxButton.YesNo,
            MessageBoxImage.Exclamation,
            MessageBoxResult.OK,
            MessageBoxOptions.ServiceNotification);
Steve
  • 976
  • 12
  • 12
  • Far better solution than spinning up a new form and marking it as top-most. Also there is the option for MessageBoxImage.None which gets rid of the cheesy image. – TChadwick Jul 12 '16 at 17:42
  • 1
    This is the only solution here that worked for me. I did have to correct some Syntax but it works great on Windows 10 .Net Core 3.1 – TheOddPerson Mar 12 '20 at 02:04
  • This works if you don't have any forms in your program. I had a program which ran by clicking an entry in a windows explorer context menu. The program had no forms and would silently execute in the background for a second. I just wanted a message box to appear on top if something went wrong. – Kwiksilver Apr 24 '20 at 19:28
2

MessageBox on top of all windows (no tray icon):

MessageBox.Show(new Form() { TopMost = true }, boxText, "Box Title",
                MessageBoxButtons.OK, boxIcon);

MessageBox and your app on top of all windows (no tray icon):

TopMost = true;
MessageBox.Show(boxText, "Box Title", MessageBoxButtons.OK, boxIcon);
TopMost = false;

MessageBox on top of all windows, plus tray icon (app loses focus):

MessageBox.Show(boxText, "Box Title", MessageBoxButtons.OK, boxIcon, 0,
                MessageBoxOptions.DefaultDesktopOnly);
// (The "0" can also be "MessageBoxDefaultButton.Button1".)

MessageBoxButtons.OK and boxIcon are optional arguments in the first two.

Setting TopLevel doesn't do anthing; it is already true.

There is no direct way to center a MessageBox on its parent form. (Except maybe centering the parent form.)

A876
  • 471
  • 5
  • 8
  • My app is in tray icon and I want the messagebox to first appear on front but no stay on top. If I click another window the messagebox should not stay on top. Also I want to open several message boxes at the same time, so when the user overs the mouse on the taskbar they can see a preview of all the messageboxes (this works with default messagebox but I can't bring it to front when it first appears). – CoolkcaH May 14 '19 at 11:32
0

The more correct way to do this is to set the owner of the MessageBox

Paul Baxter
  • 1,054
  • 12
  • 22