|
The Return Value of a Message Box
|
|
Besides displaying a message,
a message box is may be meant to let the user make a decision by clicking a
button and, depending on the button the user would have clicked, the message
box would return a value. The value returned by a message box corresponds to
the particular button the user would have clicked (on the message box). The
return values are defined in the DialogResult enumeration. The
buttons and the returned values are as follows:
|
If the User Clicks |
The Method Returns |
|
DialogResult.Abort |
 |
DialogResult.Cancel |
 |
DialogResult.Ignore |
|
DialogResult.No |
 |
DialogResult.OK |
|
DialogResult.Retry |
|
DialogResult.Yes |
|
The Message of a Message Box
|
|
The .NET Framework provides the MessageBox class
function used to easily create a message box. To display a simple message
with just an OK button, you can call the Show() static method of this
class. Its syntax is as follows:
public static DialogResult MessageBox.Show(string message);
In this case, the message to display must be passed as a
string to the Show() method. Here is an example:
public class Program
{
static int Main()
{
MessageBox.Show("Welcome to the Wonderful World of Visual C#");
return 0;
}
}
This would produce:
The message to display can be made of up to 1024
characters. To display the message on multiple lines, you can use the new
line escape sequence anywhere inside the string.
|
The Caption of a Message Box
|
|
In reality, the MessagBox.Show() method is
overloaded with various versions. Another version is:
public static DialogResult Show(string text, string caption);
This version allows you to specify a custom caption for
the message box. With this version, the first argument is the string that
the user will see displaying on the message box. You can pass it as a
string. You can also create it from other pieces of strings.
The second argument, caption, will be the
sentence to display in the title bar of the message box. Here is an example:
public class Program
{
static int Main()
{
MessageBox.Show("Welcome to the Wonderful World of Visual C#",
"Visual C# Tutorials");
return 0;
}}
This would produce:
|
The Buttons of a Message Box
|
|
Another version of the MessageBox.Show() method
is as follows:
public static DialogResult Show(string text,
string caption,
MessageBoxButtons buttons);
This version allows you to display one or more buttons
on the message box. The available buttons are defined through the
MessageBoxButtons enumeration. Its members are:
|
MessageBoxButtons |
Display |
| OK |
 |
| OKCancel |
 |
| YesNo |
|
| YesNoCancel |
 |
| RetryCancel |
 |
| AbortRetryIgnore |
 |
To use any of these combinations of buttons, call the
MessageBoxButtons enumeration and access the desired combination. Here
is an example:
public class Program
{
static int Main()
{
MessageBox.Show("Welcome to the Wonderful World of Visual C#",
"Visual C# Tutorials",
MessageBoxButtons.OKCancel);
return 0;
}
}
This would produce:
|
The Icon of a Message Box
|
|
This version allows you to display an icon. The possible
icons are available through the MessageBoxIcon enumeration. The
members of this enumerator are:
|
MessageBoxIcon |
Description |
| None |
|
|
| Asterisk |
 |
 |
| Error |
 |
 |
| Exclamation |
 |
 |
| Hand |
 |
 |
| Information |
 |
 |
| Question |
 |
 |
| Stop |
 |
 |
| Warning |
 |
 |
Here is an example:
public class Program
{
static int Main()
{
MessageBox.Show("Your order appears to be correct" +
"\nAre you ready to provide your credit card information?",
"Customer Order Processing",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information);
return 0;
}
}
This would produce:
|
The Default Button of a Message Box
|
|
When a message box is configured to display more than
one button, the operating system is set to decide which button is the
default. The default button has a thick border that sets it apart from the
other button(s). If the user presses Enter, the message box would behave as
if the user had clicked the default button. If the message box has more than
one button, you can decide what button would be the default. To specify the
default button, the MessageBox.Show() method provides the following
version:
public static DialogResult Show(string text,
string caption,
MessageBoxButtons buttons,
MessageBoxIcon icon,
MessageBoxDefaultButton defaultButton);
Based on this, you can specify the default button using
the last argument that provides values through the
MessageBoxDefaultButton enumerator whose values are:
Button1: The left button will be the default.
Here is an example:
public class Program
{
static int Main()
{
MessageBox.Show("Your order appears to be correct" +
"\nAre you ready to provide your credit card information?",
"Customer Order Processing",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
return 0;
}
}
|
|
Button2: If the message box displays two buttons,
the right button will be the default. If the message box displays three
buttons, the middle button will be the default. Here is an example:
public class Program
{
static int Main()
{
MessageBox.Show("Your order appears to be correct" +
"\nAre you ready to provide your credit card information?",
"Customer Order Processing",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button2);
return 0;
}
}
|
|
Button3: The right button will be the default.
Here is an example:
public class Program
{
static int Main()
{
MessageBox.Show("Your order appears to be correct" +
"\nAre you ready to provide your credit card information?",
"Customer Order Processing",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button3);
return 0;
}
}
|
|
|