MessageBox in C#
In this c#.net post we will learn how to display MessageBox in windows form with an example.
C# MessageBox in windows application used to display a message on form with given text message and action buttons. We can also add some additional option such as a title text, conditional button, or help button in MessageBox.
Simple MessageBox
The simple MessageBox displays text with OK button. When we click ok button the MessageBox will disappears from form.
Here, we can see the code for create simple MessageBox with an example.
We have a button control on windows form, we will get MessageBox with “Hello MeeraAcademy” message while click button.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello MeeraAcademy");
}
Here, is the output of above MessageBox example.
MessageBox with Title
We can display Message Box with text message and title.
Here is the code of display MessageBox with title.
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello MeeraAcademy", "MeeraAcademy");
}
The output of above code.
MessageBox with Buttons
In above example by default there is only one OK button display. We can display different combination of buttons in MessageBox such as OKCancel, YesNo, YesNoCancel. The MessageBoxButtons enumeration represents the buttons to be displayed on MessageBox.
MessageBoxButtons
- AbortRetryIgnore
- OK
- OKCancel
- RetryCancel
- YesNo
- YesNoCancel
Here is the example of display MessageBox with YesNo button with title.
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure to quit?", "MeeraAcademy",MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
MessageBox.Show("Bye bye");
}
else
{
MessageBox.Show("Welcome back");
}
}
Above example first MessageBox will display when click button, it display a message “Are you sure to quit?” with two buttons Yes and No. If we click on Yes button it will create new MessageBox with text “Bye Bye” and if we click on No button it will display MessageBox with “Welcome back” message.
If we use OKCancel Button with above example it should display like:
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure to quit?", "MeeraAcademy",MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
MessageBox.Show("Bye bye");
}
else
{
MessageBox.Show("Welcome back");
}
}
Icons with MessageBox
We can display icon on MessageBox dialog. The MessageBoxIcon enumeration represents an icon to be displayed on MessageBox.
MessageBoxIcon
- Asterisk
- Error
- Exclamation
- Hand
- Information
- None
- Question
- Stop
- Warning
Here we see an example of Question. write MessageBoxIcon.Question after MessageBoxButtons.OKCancel and then run program.
private void button1_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure to quit?", "MeeraAcademy",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (result == DialogResult.OK)
{
MessageBox.Show("Bye bye");
}
else
{
MessageBox.Show("Welcome back");
}
}
We hope that this windows application tutorial helped you to understand about how to create different types of MessageBox.
Next, c# windows application tutorial we will learn about Show() and ShowDialog() methods.