Private void btnSimpleMessageBox_Click(object sender, RoutedEventArgs e) Public partial class MessageBoxSample : Window The complete exampleĪs promised, here's the complete example used in this article: Notice on the screenshot how the "No" button is slightly elevated, to visually indicate that it is selected and will be invoked if the Enter or Space button is pressed. Parameter to the MessageBox.Show() method though: MessageBox.Show("Hello, world?", "My App", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No)
You can change this behavior using a fifth The MessageBox will select a button as the default choice, which is then the button invoked in case the user just presses Enter once the dialog is shown.įor instance, if you display a MessageBox with a "Yes" and a "No" button, "Yes" will be the default answer.
The names should say a lot about how they look, but feel free to experiment with the various values or have a look at this MSDN article, where each value Using the MessageBoxImage enumeration, you can choose between a range of icons for different situations. The MessageBox has the ability to show a pre-defined icon to the left of the text message, by using a fourth parameter: MessageBox.Show("Hello, world!", "My App", MessageBoxButton.OK, MessageBoxImage.Information)
MessageBox.Show("Nevermind then.", "My App") īy checking the result value of the MessageBox.Show() method, you can now react to the user choice, as seen in the code example as well as on the MessageBox.Show("Oh well, too bad!", "My App") MessageBox.Show("Hello to you too!", "My App") Here's an example: MessageBoxResult result = MessageBox.Show("Would you like to greet the world with a \"Hello, world\"?", "My App", MessageBoxButton.YesNoCancel) The MessageBoxResult enumeration that you can use. Now with multiple choices, you need a way to be able to see what the user chose, and fortunately, the MessageBox.Show() method always returns a value from Theįollowing values, which should be self-explanatory, can be used: You control which buttons are displayed by using a value from the MessageBoxButton enumeration - in this case, a Yes, No and Cancel button is included. Also notice how I use multiple lines in this message, by using a line break character (\n): MessageBox.Show("This MessageBox has extra options.\n\nHello, world?", "My App", MessageBoxButton.YesNoCancel) Parameter allows us to specify the title: MessageBox.Show("Hello, world!", "My App") MessageBox with extra buttonsīy default, the MessageBox only has the one Ok button, but this can be changed, in case you want to ask your user a question and not just show a piece of The above example might be a bit too bare minimum - a title on the window displaying the message would probably help. In its simplest form, the MessageBox just takes a single parameter, which is the message to be displayed: MessageBox.Show("Hello, world!") MessageBox with a title In the end of the article, you can find a complete example which lets you test all the variations.
We'll be going through all the various forms in this article, with each variation represented by the MessageBox.Show() line and a screenshot of the The MessageBox is used by calling the static Show() method, which can take a range of different parameters, to be able to look and behave the way you want User, and then offer one or several ways for the user to respond to the message. Its sole purpose is to show a message to the WPF offers several dialogs for your application to utilize, but the simplest one is definitely the MessageBox.