A message box or dialog box is used to interact with the users of your application.
- The purpose of using a message box may include notifying about a particular action e.g. success message after entering a record.
- Similarly, an error message if an operation is unsuccessful. In both cases, you may display the “OK” button in the message box with the message.
- In other cases, you may display a message box for the user confirmation before performing a critical action e.g. deleting a record permanently. In that case, a Yes/No button in the dialog box makes sense.
In this tutorial, I will show you how to create various types of C# message boxes with code.
How to make message box work in Visual Studio?
If you are working in Visual Studio then you have to include the System.Windows.Forms by following this:
- In your project name under “Solution Explorer” right-click and select “Add Reference”.
- There you can see a few tabs. Select “.Net” tab.
- Now locate the System.Windows.Forms and press OK
The example of creating a simple message box
After taking the above steps, you need to include the System.Windows.Forms namespace as used in the code below.
You may provide a message to be displayed and the title of the dialog box by using the MessageBox class’s show method.
The following example simply displays a box with the OK button:
using System; using System.Windows.Forms; class msgbox_example { static void Main() { string box_msg = "A Message Box with OK Button"; string box_title = "Message Box Demo"; MessageBox.Show(box_msg, box_title); } }
The result:
An example of a Yes/No message box
The Yes/No dialog box can be useful for asking users to choose an option between the two.
For example, “Are you sure you want to delete this record?” Similarly, “Do you want to quit?” and so on.
The example below shows a Yes/No message box:
using System; using System.Windows.Forms; class msgbox_example { static void Main() { string box_msg = "Message Box with Yes / No Options"; string box_title = "Yes No Dialog"; MessageBox.Show(box_msg, box_title, MessageBoxButtons.YesNo); } }
The result:
Adding the Cancel button in the above dialog
The code below shows how with the output:
using System; using System.Windows.Forms; class msgbox_example { static void Main() { string box_msg_y_n_c = "Yes/No/Cancel Options"; string box_title_y_n_c = "Yes No Cancel Dialog box"; MessageBox.Show(box_msg_y_n_c, box_title_y_n_c, MessageBoxButtons.YesNoCancel); } }
Adding an icon example
By using the MessageBoxIcon Enum, you may specify an icon to be displayed with the message in the dialog box.
Different icons can be used by specifying respective value. For example:
- Exclamation
- Information
- Question
- Warning
- Asterisk
- Error
- Hand
- Stop
- None
The following example shows a few message boxes with different icons:
using System; using System.Windows.Forms; class msgbox_example { static void Main() { MessageBox.Show("Exclamation Icon message box", "Dialog with Icon", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); MessageBox.Show("Warning Icon message box", "Dialog with Icon", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); MessageBox.Show("There was an error processing your request!", "Dialog with Icon", MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBox.Show("Operation cannot be successful, Do you want to retry?", "Dialog with Icon", MessageBoxButtons.RetryCancel, MessageBoxIcon.Stop); } }
As you execute this code, four dialog boxes should display with respective icons and messages.
Performing actions on different buttons
The message box with the OK button only may be used for information purposes to the user, without performing any action.
However, in the case of boxes with OK, Cancel, Yes and No, and Retry buttons, you may require performing some action based on the user’s selection.
For that, you may use a variable that catches the returned value. Then it can be used in an if statement and you may perform the desired action.
To demonstrate that, the following program displays a message box with Yes/No/Cancel buttons.
As you click any button, another message box with an “OK” button is displayed with its message for each button:
using System; using System.Windows.Forms; class msgbox_example_value { static void Main() { var selectedOption = MessageBox.Show("Please Select a button?", "Dialog Value Demo", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); // If the no button was pressed ... if (selectedOption == DialogResult.Yes) { MessageBox.Show("Yes is pressed!", "Yes Dialog", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (selectedOption == DialogResult.No) { MessageBox.Show("No is pressed!", "No Dialog", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show("Cancel is pressed", "Cancel Dialog", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }