C# MessageBox To Front When App is Minimized To Tray

∥☆過路亽.° 提交于 2019-11-28 00:02:52

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);
Cody Gray

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!