In general, the message box is used to display an alert or message or confirmation to the end users.
A normal message box has only one option like below.
Message box is nothing but dialogue box.
Example
Create a new window then drag and drop the button control from the toolkit in that window.
Now write the following runtime code for that button control under the click event:
[csharp]
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to SPlessons");
}
[/csharp]
Now run the application.The output will be as shown below.
Click on Message Box button then the result is:
If clicked on OK, then the message box is closed.
More Info
If needed to place a title for message box then write the following code under the click event for button control.
[csharp] private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to SPlessons","Confirmation");
}[/csharp]
Now run the application and click on Message box button control.The output appears as shown below.
Message Box with Different Buttons
Description
The MessageBoxButton has different options like Yes, No, and Cancel.
By default, only one option can be shown using message box. If wanted to add more options in the message box then follow the below example.
Example
Now check with example.
Create a new window and write the following code under the click event:
[csharp] private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to SPlessons","Confirmation", MessageBoxButton.YesNoCancel);
}[/csharp]
Now run the application and click on Message box button control.
MessageBox with Title, Icon, OK, and Cancel Buttons
Example
Create a new window and write the following code under the click event:
[csharp] private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to SPlessons","Confirmation", MessageBoxButton.OKCancel, MessageBoxImage.Informatio);
}[/csharp]
Now run the application and click on Message box button control.
For MessageBoxImage, by default 9 options are present. Those are: