C# MessageBox To Front When App is Minimized To Tray
Asked Answered
N

5

12

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();                
}
Newsom answered 29/1, 2011 at 2:50 Comment(2)
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.Newsom
https://mcmap.net/q/276318/-c-messagebox-to-front-when-app-is-minimized-to-tray is better solution ?Manchineel
V
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);
Variole answered 29/1, 2011 at 3:1 Comment(3)
great! when I close the message box it would then kill that form too, right? (it looks like it does)Newsom
@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.)Kung
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.Raggletaggle
K
33

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
Kung answered 29/1, 2011 at 3:25 Comment(5)
it still doesnt come to front or top. i am not able to drag that messagebox around or click on the ok/close buttons.Megacycle
@shakthi apparently TopMost doesn't work on vista. maybe this is your problem, see: msdn.microsoft.com/en-us/library/windows/desktop/…Overbalance
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?Nephogram
This doesn't work in a WPF app. You get an exception that the MessageBoxOptions enum doesn't like the 0x40000 value.Karolinekaroly
@Peter pete - you should be able to use this as the parameter in vb.net: CType(&H40000, MessageBoxOptions). Worked a treat for me.Vaporescence
V
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);
Variole answered 29/1, 2011 at 3:1 Comment(3)
great! when I close the message box it would then kill that form too, right? (it looks like it does)Newsom
@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.)Kung
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.Raggletaggle
M
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);
Methodical answered 15/4, 2016 at 18:1 Comment(3)
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.Tocopherol
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.1Dutiable
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.Phoney
L
3

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.)

Landowska answered 28/7, 2017 at 21:10 Comment(1)
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).Floatplane
P
0

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

Poop answered 15/4, 2016 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.