Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF- MessageBox to be top most

Tags:

c#

wpf

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?

like image 383
DasDas Avatar asked Dec 05 '22 03:12

DasDas


2 Answers

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);
like image 78
George Avatar answered Feb 17 '23 10:02

George


  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

like image 42
TGarrett Avatar answered Feb 17 '23 10:02

TGarrett