7.4 Displaying Message Boxes

 <  Day Day Up  >  

You want to display a message to a user using a message box.


Technique

Use the static method Show defined within the MessageBox class. This method is overloaded several times, allowing you to customize the display of the message as you see fit. The simplest message box contains a single string parameter denoting the message to display. To add a caption to the message box, insert an additional string parameter. You can control which buttons are displayed in the third parameter by using the MessageBoxButtons enumerated data type. You can use a fourth parameter to display an icon. You specify this icon using a value from the MessageBoxIcon enumerated data type. To specify the default button, use a value from the MessageBoxDefaultButton enumerated data type as the fifth parameter. Finally, you specify additional options in the sixth parameter by using the logical or operator ( ) on values within the MessageBoxOptions enumeration. Table 7.1 shows the possible values for the enumerated data types discussed in this recipe.

Table 7.1. MessageBox Enumerated Data Types

Data Type

Value

Description

MessageBoxButtons

AbortRetryIgnore

Message box containing three buttons labeled Abort , Retry , and Ignore

 

OK

A single button labeled OK

 

OKCancel

Displays an OK button followed by a Cancel button

 

RetryCancel

Shows two buttons labeled Retry and Cancel

 

YesNo

Displays a Yes button and a No button

 

YesNoCancel

Displays Yes , No , and Cancel buttons

MessageBoxIcon

Asterisk

Displays a lowercase i inside a circle in the corner of the message box

 

Error

Icon is a white X inside a red circle

 

Exclamation

Shows an exclamation point within a yellow triangle

 

Hand

Shows same icon as Error value

 

Information

Uses the same icon shown when using the Asterisk value

 

None

Does not display an icon

 

Question

Icon consists of a question mark within a circle

Stop

Same as Error value, a white X inside a red circle

Warning

Icon is the same as the Exclamation value

MessageBoxDefaultButton

Button1

Gives the first button the initial focus and renders a focus rectangle around the button

 

Button2

Makes the second button, if one exists, the default button

 

Button3

Makes the third button the default button

MessageBoxOptions

DefaultDesktopOnly

Displays the message box on the current desktop

 

RightAlign

Aligns the text along the right edge of the message box

 

RtlReading

Renders the text in the message box in right-to-left order

ServiceNotification

Message box is displayed on the active desktop and will be shown even if a user is not logged on

Comments

Sometimes, all you need to do is display a simple message to the user or ask her a simple question. Message boxes handle this task, freeing you from having to create new forms. The MessageBox class contains a single static method, as well as the standard System.Object methods , named Show . There are 12 overloaded versions of this method corresponding to the level of customization you want to perform on the message box. In its most basic form, a message box can contain a single message and an OK button to close the message box. To show a message box with a single message, use the Show method with a single string parameter, and if you want to display a caption on the message box, create a second string parameter for that purpose:

 
 MessageBox.Show( "Simple Message Box" ); MessageBox.Show( "Simple Message Box", "With a caption" ); 

As mentioned earlier, you can customize the message box further by controlling the types of buttons and icons displayed, controlling which button is the default button, and specifying different message-box options. Based on the buttons that you elected to display on the message box, the value returned will be a value from the DialogResult enumerated data type. You can compare the value returned from the Show method with a value from the DialogResult type to determine the action performed by the user. The following code listing displays a message box using all the available customization options. Figure 7.2 shows the resulting message box:

 
 if( MessageBox.Show( "Simple Message Box",     "With a caption, default button, icon and RTL reading",     MessageBoxButtons.OKCancel,     MessageBoxIcon.Information,     MessageBoxDefaultButton.Button2,     MessageBoxOptions.RtlReading      MessageBoxOptions.RightAlign ) == DialogResult.Cancel )     {         return;     } 
Figure 7.2. The MessageBox.Show method contains several overloaded methods to customize the display of the message box.

graphics/07fig02.gif

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net