Search code examples
wpf

How to get MessageBox.Show() to pop up in the middle of my WPF application?


I have a 700w x 300h WPF application and can drag it anywhere on my large screen.

When my application executes:

MessageBox.Show("Sorry, this function is not yet implemented.");

the mesage box appears in the middle of my screen, which may or may not even be near the application itself.

How can I get my MessageBox to appear in the middle of my application instead?


Solution

  • If your app has several windows, you might want to use Application.Current.MainWindow:

    MessageBox.Show(Application.Current.MainWindow, "Can't login with given names.", "Login Failure", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.Cancel);