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 it to. 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 result. In the end of the article, you can find a complete example which lets you test all the variations.
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
The above example might be a bit too bare minimum - a title on the window displaying the message would probably help. Fortunately, the second and optional parameter allows us to specify the title:MessageBox.Show("Hello, world!", "My App");
MessageBox with extra buttons
By 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 information. 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);
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. The
following values, which should be self-explanatory, can be used:
- OK
- OKCancel
- YesNoCancel
- YesNo
MessageBoxResult result = MessageBox.Show("Would you like to greet the world with a \"Hello, world\"?", "My App", MessageBoxButton.YesNoCancel);
switch(result)
{
case MessageBoxResult.Yes:
MessageBox.Show("Hello to you too!", "My App");
break;
case MessageBoxResult.No:
MessageBox.Show("Oh well, too bad!", "My App");
break;
case MessageBoxResult.Cancel:
MessageBox.Show("Nevermind then...", "My App");
break;
}
By 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 screenshots.
MessageBox with an icon
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);
Using the MessageBoxImage enumeration, you can choose between a range of icons for different situations. Here's the complete list:
- Asterisk
- Error
- Exclamation
- Hand
- Information
- None
- Question
- Stop
- Warning
MessageBox with a default option
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. For instance, if you display a MessageBox with a "Yes" and a "No" button, "Yes" will be the default answer. You can change this behavior using a fifth parameter to the MessageBox.Show() method though:MessageBox.Show("Hello, world?", "My App", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
No comments:
Post a Comment