Message Box C#
By FoxLearn 1/15/2025 9:17:33 AM 218
C# MessageBox
The MessageBox
class has an overloaded static Show
method that displays a message box with a specified message and action buttons. These buttons can include options like OK and Cancel, Yes and No, etc.
Below are some of the available options that can be used in a C# message box.
For example, message box c#
using System; using System.Windows.Forms; class Program { static void Main() { // Display a message box with a message and an OK button MessageBox.Show("Hello, World!", "Message Title", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
In this example: MessageBox.Show()
is used to display a message box.
- The first parameter is the message that will be displayed.
- The second parameter is the title of the message box.
- The third parameter specifies which buttons to display (e.g.,
MessageBoxButtons.OK
,MessageBoxButtons.YesNo
). - The fourth parameter specifies the icon to display in the message box (e.g.,
MessageBoxIcon.Information
,MessageBoxIcon.Error
).
C# Simple MessageBox
The most basic form of a MessageBox
displays a dialog with a message and an OK button. When the user clicks the OK button, the dialog box closes.
MessageBox.Show("This is a simple MessageBox.");
In this case, the MessageBox.Show()
method is used to display a message with a default OK button.
C# MessageBox with Title
Creating a simple MessageBox
with both a message and a title:
MessageBox.Show("This is a simple MessageBox.", "Message Box Title");
In this case, the MessageBox.Show()
method displays the message along with a custom title for the message box.
C# MessageBox with Buttons
A MessageBox
can display various button combinations, such as Yes/No or OK/Cancel. These combinations are defined in the MessageBoxButtons
enumeration, which includes the following values:
- OK
- OKCancel
- AbortRetryIgnore
- YesNoCancel
- YesNo
- RetryCancel
Here’s an example of a MessageBox
with a title and Yes/No buttons. This type of message box is typically used when you want to ask the user if they want to close an application. Based on the user's choice, the application can either close or perform another action. The Show
method returns a DialogResult
enumeration, which you can use to handle the user's response.
DialogResult result = MessageBox.Show("Do you want to close this window?", "Close Window", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { this.Close(); } else { // Perform an alternate action }
In this example, if the user clicks "Yes," the window will close. If "No" is selected, a different action can be taken.
C# MessageBox with Icon
A MessageBox
can also display an icon to visually convey the type of message. The MessageBoxIcon
enumeration represents different icons that can be shown in a MessageBox
, with the following values:
- None
- Hand
- Question
- Exclamation
- Asterisk
- Stop
- Error
- Warning
- Information
For example, how to create a MessageBox
with a title, buttons, and an icon:
DialogResult result = MessageBox.Show("Do you want to abort this operation?", "Abort Operation", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning); if (result == DialogResult.Abort) { this.Close(); } else if (result == DialogResult.Retry) { // Retry the operation } else { // Perform an alternate action }
In this example, a warning icon is displayed along with the buttons "Abort", "Retry", and "Ignore." The user's response is captured using the DialogResult
enumeration, and based on that, different actions are taken.
C# MessageBox with Default Button
ou can also set a default button in a MessageBox
, which determines which button is selected by default when the dialog appears. By default, the first button is selected, but you can change it using the MessageBoxDefaultButton
enumeration, which has the following values:
- Button1
- Button2
- Button3
For example, how to create a MessageBox
with a title, buttons, an icon, and sets the second button as the default:
DialogResult result = MessageBox.Show(message, "Do you want to abort this operation?", "Abort Operation", MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2); if (result == DialogResult.Abort) { this.Close(); } else if (result == DialogResult.Retry) { // Retry the operation } else { // Perform an alternate action }
In this example, the "Retry" button is set as the default button, and the MessageBox
will focus on it when it appears. Based on the user's selection, different actions are triggered.
C# MessageBox with Message Options
The MessageBoxOptions
enumeration provides various options that can modify the appearance and behavior of a MessageBox
. It includes the following values:
ServiceNotification
DefaultDesktopOnly
RightAlign
RtlReading
For example, how to create a MessageBox
with multiple options, including right alignment and right-to-left reading:
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
In this example, the MessageBox
is displayed with both right alignment and right-to-left reading order, which is useful for languages that read from right to left, such as Arabic or Hebrew. The MessageBoxOptions
enumeration is combined using the bitwise OR (|
) operator to apply both options simultaneously.
C# MessageBox with Help Button
A MessageBox
can also include a Help button, which is useful when you want to provide users with additional information or a help file.
For example, how to create a MessageBox
with a Help button:
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, MessageBoxOptions.RightAlign, true);
In this example, the true
parameter is used to enable the Help button.
You can also associate a help file to be displayed when the Help button is clicked. Here's an example where a .chm
help file is referenced:
DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 0, "helpfile.chm");
In this case, the MessageBox
will show a question icon, and when the Help button is clicked, it will reference the helpfile.chm
help file.
- Using the OrderBy and OrderByDescending in LINQ
- Querying with LINQ
- Optimizing Performance with Compiled Queries in LINQ
- MinBy() and MaxBy() Extension Methods in .NET
- SortBy, FilterBy, and CombineBy in NET 9
- Exploring Hybrid Caching in .NET 9.0
- Using Entity Framework with IDbContext in .NET 9.0
- Primitive types in C#