Welcome the this tutorial about MessageBoxesIn this tutorial i’ll show how to create a MessageBox with different buttons and icons.
At first let’s see what different kind of buttons exist:
- OK
- OK, Cancel
- Yes, No
- Yes, No, Cancel
- Retry, Cancel
- Abort, Retry, Ignore
And now what different kind of icons exist:

Error

Information

Question

Warning
A messagebox can be shown to show the user an information, or to let the user make a decision.
Per example: An easy information message would be “Text saved” with an Ok button.
A messagebox with a dicision to make would be “ Do you really want to quit?” with an Yes and No button.
Retrun the value of a messagebox:If the user clicks a button, the program gets a DialogResult.
Here are the different return values and the buttons for the values:
- OK --- DialogResult.OK
- Yes --- DialogResult.Yes
- No --- DialogResult.No
- Abort --- DialogResult.Abort
- Retry --- DialogResult.Retry
- Cancel --- DialogResult.Cancel
- Ignore --- DialogResult.Ignore
So let’s create some messageboxes:
( to show a messagebox use the Show() method )
We create a simple messagebox with a text, caption, Ok button and an information symbol:
At the beginning of every messagebox there is a event, in this case it is a button
Let’s create a button which should show a welcome massage.. double click on the button to insert a code.
There are 2 ways to write the code for a MessageBox:
1.
CODE
public void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("hello and welcome!", "welcome", MessageBoxButtons.OK, MessageBoxIcon.Information)
}
As we see the structure of this messagebox is the following:
MessageBox.Show("text", "caption", "buttons", "icon")
2.
CODE
public void button1_Click(object sender, EventArgs e)
{
string message = "hello and welcome"; // Message-Text
string caption = "welcome"; // TitleBar Text
MessageBoxButtons buttons = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Information;
MessageBox.Show(message, caption, buttons, icon)
}
If we have more than one button we can set a defaultbutton.
In an example it would look like: (yes no - buttons)
CODE
public void button1_Click(object sender, EventArgs e)
{
string message = "Do you really want to quit?";
string caption = "exit";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxIcon icon = MessageBoxIcon.Question;
MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button2;
MessageBox.Show(message, caption, buttons, icon, defaultbutton)
}
MessageBoxDefaultButton.Button1 (the first button) Yes is default
MessageBoxDefaultButton.Button2 (the second button) No is default
Now we look at how to handle a result:So we create a messagebox with 2 buttons and the user has to decide what to do.
We create a quit-button:
CODE
public void button1_Click(object sender, EventArgs e)
{
string message = "Do you really want to quit?";
string caption = "exit";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxIcon icon = MessageBoxIcon.Question;
MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button2;
DialogResult result;
result = MessageBox.Show(message, caption, buttons, icon)
if (result == DialogResult.Yes)
{
Close();
}
}
If there is no definied event on a dialogresult, the messagebox will always close and nothing happens. So we don’t need to write an event for DialogResult.No in this example.
Now we can create a MessageBox with text, caption, buttons, icon and know how to get a result and how to handle the result.
At least we talk about the different overloaded Show methods
We already used 3 overloaded Show methods in this tutorial
Show(String)in example:CODE
public void button1_Click(object sender, EventArgs e)
{
string message = "Welcome";
MessageBox.Show(message);
}
Show(String, String)in example:CODE
public void button1_Click(object sender, EventArgs e)
{
string message = "Hello and welcome!";
string caption = "Welcome";
MessageBox.Show(message, caption);
}
Show(String, String, MessageBoxButton)in example:CODE
public void button1_Click(object sender, EventArgs e)
{
string message = "Hello and welcome";
string caption = "Welcome";
MessageBoxButtons buttons = MessageBoxButtons.OKCancel;
MessageBox.Show(message, caption, buttons);
}
Show(String, String, MessageBoxButton, MessageBoxIcon)This is the first example we used.
Show(string, string, MessageBoxButton, MessageBoxIcon, MessageBoxDefaultButton)This is the second example we used.
Show(String, String, MessageBoxButton, MessageBoxIcon, MessageBoxDefaultButton , MessageBoxResult)This is the third example we used.
Of course you don't have to use MessageBoxIcon and MessageBoxDefaultButton to use MessageBoxResult!
You can mix it a little bit, but for MessageBoxResult, of course, you need min. 2 buttons.

So, you are also able to code:
Show(String, String, MessageBoxButtons, MessageBoxResult)CODE
public void button1_Click(object sender, EventArgs e)
{
string message = "Message - Text"; // Message
string caption = "Title - Text"; // Caption
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxDefaultButton defaultbutton = MessageBoxDefaultButton.Button2;
DialogResult result;
result = MessageBox.Show(message, caption, buttons)
if (result == DialogResult.Yes)
{
// Event here
}
}
Thank you very much for reading

Kindly regards
Benjamin Ihde