6.5. Implementing the MessageBox Pop-Up Dialog

The MessageBox class allows for the easy generation of a pop-up dialog box. Typically it consists of message text, one or more buttons, and an optional icon such as the information icon pictured in Figure 6.2.

To display the MessageBox dialog, we invoke an instance of its static member function Show(). For example, the following invocation generated the Bad Data Dialog pictured in Figure 6.3:

string messages = "Please enter your name before pressing OK";
string title    = "Bad Data Dialog";

if ( textBox1.Text == string.Empty )
     MessageBox.Show( message, title,
                      MessageBoxButtons.OK,
                      MessageBoxIcon.Warning );

We invoke a four-parameter instance of Show(). The first parameter is the string displayed within the dialog box. The second parameter is the string displayed in the title bar of the dialog box. The third parameter represents one of the enum MessageBoxButtons values. This value determines the number and text of buttons displayed in the message. The following six enumerators are currently defined:

1.
AbortRetryIgnore creates Abort, Retry, and Ignore buttons.

2.
OK creates an OK button.

3.
OKCancel creates OK and Cancel buttons.

4.
RetryCancel creates Retry and Cancel buttons.

5.
YesNo creates Yes and No buttons.

6.
YesNoCancel creates Yes, No, and Cancel buttons.

The fourth parameter represents one of the enum MessageBoxIcon values. This value determines the icon displayed in the message box. For example, Warning is represented by a black exclamation point nested in a yellow triangle. Other icon values include the following:

  • Asterisk is represented by a lowercase i in a circle.

  • Error is represented by a white x in a red circle (pictured in Figure 6.4).

  • Information is represented by a blue i in a white dialog bubble (pictured in Figure 6.2).

  • Question is represented by a blue question mark, also in a white dialog bubble.

Show() can be invoked with simply a string to be displayed:

MessageBox.Show( "OK: bye!" );

The resulting display box in this case is pictured in Figure 6.5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.217.5