C# Message Box

In C#, a MessageBox is a pre-built dialog box that displays a message to the user and typically requires some sort of user interaction, such as clicking a button or entering input. The MessageBox class is part of the System.Windows.Forms namespace, which provides a variety of graphical user interface (GUI) components for building Windows desktop applications.

C# MessageBox

Following is an example of a simple C# MessageBox program that displays a message to the user and waits for the user to click the OK button:

using System; using System.Windows.Forms; namespace MessageBoxExample { class Program { static void Main(string[] args) { // Display a message box with a message and a title MessageBox.Show("Hello, World!", "Message"); // Wait for the user to click the OK button before exiting Application.Run(); } } }

Here's what this program does:

How to create C# messegae box
  1. It imports the System and System.Windows.Forms namespaces.
  2. It defines a Program class.
  3. It defines a Main method that is called when the program starts.
  4. In the Main method, it calls the MessageBox.Show method to display a message box with the message "Hello, World!" and the title "Message".
  5. It calls the Application.Run method to start a message loop and wait for the user to click the OK button.

When you run this program, a message box will appear with the message "Hello, World!" and the title "Message". The program will wait for the user to click the OK button before exiting.

C# MessageBox with Yes/No buttons

You can create a MessageBox with Yes/No buttons by using the MessageBoxButtons.YesNo argument in the MessageBox.Show() method:

using System.Windows.Forms; class Program { static void Main() { DialogResult result = MessageBox.Show("Do you want to continue?", "Confirmation", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { MessageBox.Show("User clicked Yes button"); } else { MessageBox.Show("User clicked No button or closed the dialog box"); } } }

In the above example, the MessageBox.Show() method displays a dialog box with the message "Do you want to continue?" and the title "Confirmation". The MessageBoxButtons.YesNo argument specifies that the dialog box should have Yes/No buttons. The DialogResult variable result stores the user's choice, which can be either DialogResult.Yes or DialogResult.No.

C# MessageBox Custom Button

MessageBox in C# provides an option to create custom buttons with a predefined set of values. To create a custom button, you can use the MessageBoxButtons enumeration. This enumeration provides different types of buttons that can be added to the MessageBox, such as "OK," "Yes," "No," "Cancel," "Retry," and "Ignore."


C# message box with yes no cancel button

Following is an example of creating a MessageBox with custom buttons:

using System.Windows.Forms; class Program { static void Main() { DialogResult result = MessageBox.Show("Are you sure you want to delete this item?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { // perform delete action } } }

C# MessageBox with New Line:

You can add a new line to the MessageBox's text by adding "\n" to the string. This will insert a new line in the text.

Following is an example of creating a MessageBox with a new line:

using System.Windows.Forms; class Program { static void Main() { MessageBox.Show("This is the first line.\nThis is the second line.", "New Line", MessageBoxButtons.OK, MessageBoxIcon.Information); } }