WPF- MessageBox to be top most - c#

I have a WPF application, i need the MessageBox to always be top most.
in win forms i would do something like that:
System.Windows.Forms.MessageBox.Show(new Form() { TopMost = true }, "sure you wanna save?", "confirm", MessageBoxButtons.YesNo)
but how do i do that in WPF?
i saw a few different answers but non of them work for me e.g:
MessageBox.Show(Application.Current.MainWindow, "Im always on top - of the main window");
My mainWindo is null.
In my application the MessageBox is opening from different pages- not windows
Any idea how i do it in the most simple way?

Use MessageBoxOptions.DefaultDesktopOnly and it keeps the MessageBox on top of the window.
MessageBox.Show("You entered an incorrect value. Try once more.", "Wrong input", MessageBoxButton.OK, MessageBoxImage.Exclamation,MessageBoxResult.OK,MessageBoxOptions.DefaultDesktopOnly);

this.Dispatcher.Invoke((Action)(() =>
{
MessageBox.Show("Im always on top - of the main window");
}));
This will run it in the UI thread.Throw this in the method that shows the msg box.
Cheers,
G

You will have to create your own Window and set its Topmost value to true.
MyWindow dialog = new MyWindow();
dialog.Topmost = true;
dialog.Show();

Related

Message Box hidden in task bar in win form

I have a win form containing only progress bar and
logic for opening and closing an excel or word document using interopt dll.
This win form runs when I click a button in my web application.
The problem is, the win form after closing document displays a Message Box.
This message box always remains in the taskbar instead of Showing up in the
screen.
PS:
DialogResult Result = MessageBox.Show("Save","Confirm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
When I give the message box in this format, the message box always remains in the taskbar. But the functionalities given as properties for the message box works fine.
DialogResult Result = MessageBox.Show("Save","Confirm", MessageBoxButtons.YesNoCancel,MessageBoxOptions.DefaultDesktopOnly, MessageBoxIcon.Question);
When I give the message box in this format, the message box does not remains in the taskbar
but the variable "Result" always returns "No".
Thanks in Advance
Try the below:
DialogResult Secondpopups = MessageBox.Show(new Form() { TopMost = true },"Message u need to diaplay", "Title of the Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

messagebox on top of splashscreen

I have a messagebox that pops-up when a user can't be loaded (in this case because he don't have a warehouse) while loading there is a splashscreen that shows that the data is being loaded.
I tried the TopMost set to true, but yeah the spalshscreen isn't the parent so it don't work, so i tried TopLevel set to true but it didn't do the trick.
So i tried:
MessageBox.Show(Splashscreen.LoadingScreen.ActiveForm, e.Message, "No warehouses", MessageBoxButtons.OK, MessageBoxIcon.Error);
but this is cross thread so I get an: InvalidOperationException
So is there another way to set the messagebox on top?
Try this it will show your MessageBox at the top of every window currently open.
MessageBox.Show(this,
"Your text",
"Settings Needed",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1,
(MessageBoxOptions)0x40000); // this is MB_TOPMOST flag
This will keep the message box on top of every window because we are passing MB_TOPMOST Value to MessageBoxOptions parameter. You can visit this for more information.
I faced similar issue - my MessageBox get hide behind of SplashScreen. Neither use of "this" adwiced here, nor "new Form" works for my WPF application. However, the construction
MessageBox.Show(msg, errType, MessageBoxButton.OK,
MessageBoxImage.Asterisk, reply, MessageBoxOptions.ServiceNotification);
with MessageBoxOptions.ServiceNotification helps, putting MessageBox on top of SplashScreen.

Messagebox.Show from a background application [duplicate]

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();
}
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
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);
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);
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.)
The more correct way to do this is to set the owner of the MessageBox

how to force user to respond to message box in c# windows application

I want to show messagebox to the user, such that the user cannot refuse to confirm the message box. User should not be allowed to do anything else in the screen until he confirms the messagebox. This is a windows based c# application. The main thing is, even if i use windows message box. Some times it is hiding behind some screen. But for my case, i want message box to be on top most whenever it appears. I am using some other third party applications, which over rides my message box. I want to overcome this. How to do this...
Invoke messagebox inside the constructor of your form.
public Form1()
{
if (MessageBox.Show(this, "Confirm?", "Attention", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{
}
else
{
}
InitializeComponent();
}
OR
Invoke another form instance using ShowDialog() method,
public Form1()
{
Form2 frm=new Form2();
frm.ShowDialog(this);
}
You might have to define a custom message box (a Form) and set its TopMost property to true.This will make it on top of ever other window, except other TopMost windows.
That's assuming you want it on top of other applications too, which I'm not sure it's what you're looking for...
The normal windows MessageBox() function should do exactly this unless I'm missing something in your question.
Always specify a owner when displaying a message box.

Having the application minimize to the system tray when button is clicked?

How can I have my application minimize itself to the system tray in WindowsXP/Vista?
I'm also looking for a way to have a message display itself when the mouse is hovered on the icon. Is it possible to have two lines in the pop up balloon?
I assume you mean minimize to the System tray because you have talked about icons and message ballons?
The following code will set up a tray icon:
private void SetUpTrayIcon()
{
notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.BalloonTipText = "Ballon minimize text";
notifyIcon.BalloonTipTitle = "Ballon minimize title";
notifyIcon.Text = "Icon hover text";
notifyIcon.Icon = new System.Drawing.Icon(
System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream("MyIcon.ico"));
notifyIcon.Click += new EventHandler(HandlerToMaximiseOnClick);
}
To show the icon in the tray (you may want to do this on the window state change event for example, do something like the following:
if (notifyIcon != null)
{
notifyIcon.Visible = true;
notifyIcon.ShowBalloonTip(2000);
}
To display a ballon on mouse hover you want to use the same code as above possibly in the mousemove for the icon.
Note: ShowBalloonTip is overloaded if you want to change the message at different points. The message the balloon displays will respect newlines eg Environment.NewLine can be added to it.
try
to minimize
this.WindowState = FormWindowState.Minimized;
to minimize to tray see this
What's the proper way to minimize to tray a C# WinForms app?
Bye
The popup balloon will display whatever is shown in the form's title bar (which is the form's .Text property). I don't know of any way to make it multi-lined (if there is a way, it's sure to be complicated and probably not worth the trouble).
This earlier question gives some answers to the basic question. Your toolbox contains a control called NotifyIcon - use this to place an icon in the system tray.

Categories

Resources