Displaying Messages Using the MessageBox.Show() Method


Displaying Messages Using the MessageBox.Show() Method

A message box is a small dialog box that displays a message to the user (just in case that wasn't obvious enough). Message boxes are often used to tell the user the result of some action, such as "The file has been copied." or "The file could not be found." A message box is dismissed when the user clicks one of the message box's available buttons. Most applications have many message boxes, but developers don't often display messages correctly. It's important to remember that when you display a message to a user, you're communicating with the user. In this section, I'm going to teach you not only how to use the MessageBox.Show() function to display messages, but how to use the statement effectively.

The MessageBox.Show() function can be used to tell a user something or ask the user a question. In addition to text, which is its primary function, you can also use this function to display an icon or display one or more buttons that the user can click. Although you're free to display whatever text you want, you must choose from a predefined list of icons and buttons.

The MessageBox.Show() method is an overloaded method. This means that the method was written with numerous constructs supporting various options. While coding in Visual C#, IntelliSense displays a drop-down scrolling list displaying any of the 21 overloaded MessageBox.Show() method calls to aid in coding. Following are a few ways to call MessageBox.Show():

To display a message box with specified text, a caption in the title bar, and an OK button, use this syntax:

MessageBox.Show(MessageText, Caption);


To display a message box with specified text, caption, and one or more specific buttons, use this syntax:

MessageBox.Show(MessageText, Caption, Buttons);


To display a message box with specified text, caption, buttons, and icon, use this syntax:

MessageBox.Show(MessageText, Caption, Buttons, Icon);


In all these statements, MessageText is the text to display in the message box, Caption determines what appears in the title bar of the message box, Buttons determines which buttons the user sees, and Icon determines what icon (if any) appears in the message box. Consider the following statement, which produces the message box shown in Figure 17.1.

MessageBox.Show("This is a message.", "Hello There");


Figure 17.1. A very simple message box.


As you can see, if you omit Buttons, Visual C# displays only an OK button. You should always ensure that the buttons displayed are appropriate for the message.

Specifying Buttons and an Icon

Using the Buttons parameter, you can display one or more buttons in the message box. The Buttons parameter type is MessageBoxButtons, and Table 17.1 shows the allowable values.

Table 17.1. Allowable Enumerators for MessageBoxButtons

Member

Description

AbortRetryIgnore

Displays Abort, Retry, and Ignore buttons

OK

Displays OK button only

OKCancel

Displays OK and Cancel buttons

YesNoCancel

Displays Yes, No, and Cancel buttons

YesNo

Displays Yes and No buttons

RetryCancel

Displays Retry and Cancel buttons


Because the Buttons parameter is an enumerated type, Visual C# gives you an IntelliSense drop-down list when specifying a value for this parameter. Therefore, committing these values to memory isn't all that important; you'll commit the ones you use most often to memory fairly quickly.

The Icon parameter determines the symbol displayed in the message box. The Icon parameter is an enumeration from the MessageBoxIcon type. Table 17.2 shows the most commonly used values of MessageBoxIcon.

Table 17.2. Common Enumerators for MessageBoxIcon

Member

Description

Exclamation

Displays a symbol consisting of an exclamation point in a triangle with a yellow background

Information

Displays a symbol consisting of a lowercase letter i in a circle

None

Displays no symbol

Question

Displays a symbol consisting of a question mark in a circle

Stop

Displays a symbol consisting of a white X in a circle with a red background

Warning

Displays a symbol consisting of an exclamation point in a triangle with a yellow background


The Icon parameter is also an enumerated type; therefore, Visual C# gives you an IntelliSense drop-down list when specifying a value for this parameter.

The message box in Figure 17.2 was created with the following statement:

MessageBox.Show("I'm about to do something...","MessageBox sample",      MessageBoxButtons.OKCancel,MessageBoxIcon.Information);


Figure 17.2. Assign the Information icon to general messages.


The message box in Figure 17.3 was created with a statement almost identical to the previous one, except that the second button is designated as the default button. If a user presses the Enter key with a message box displayed, the message box acts as though the user clicked the default button. You'll want to give careful consideration to the default button in each message box. For example, if the application is about to do something that the user probably doesn't want to do, it's best to make the Cancel button the default buttonin case the user is a bit quick when pressing the Enter key. Following is the statement used to generate the message box in Figure 17.3:

MessageBox.Show("I'm about to do something irreversible...",       "MessageBox sample",       MessageBoxButtons.OKCancel,MessageBoxIcon.Information,       MessageBoxDefaultButton.Button2);


Figure 17.3. The default button has a dark border.


The Error icon is shown in Figure 17.4. The Error icon is best used in rare circumstances, such as when an exception has occurred. Overusing the Error icon is like crying wolfwhen a real problem emerges, the user might not take notice. Notice here how I've only displayed the OK button. If something has already happened and there's nothing the user can do about it, don't bother giving the user a Cancel button. The following statement generates the message box shown in Figure 17.4:

MessageBox.Show("Something bad has happened!","MessageBox sample",       MessageBoxButtons.OK, MessageBoxIcon.Error);


Figure 17.4. If users have no control over what has occurred, don't give them a Cancel button.


In Figure 17.5, a question has been posed to the user, so the message displays the Question icon. Also note how the message box assumes that the user would probably choose No, so the second button is set as the default. In the next section, you'll learn how to determine which button the user clicks. Here's the statement used to generate the message box shown in Figure 17.5:

MessageBox.Show("Would you like to format your hard drive now?",    "MessageBox sample",MessageBoxButtons.YesNo,MessageBoxIcon.Question,    MessageBoxDefaultButton.Button2);


Figure 17.5. A message box can be used to ask a question.


As you can see, designating buttons and icons isn't all that difficult. The real effort comes in determining which buttons and icons are appropriate for a given situation.

Determining Which Button Is Clicked

You'll probably find that many of your message boxes are simple, containing only an OK button. For other message boxes, however, you must determine which button a user clicks. Why give the user a choice if you're not going to act on it?

The MessageBox.Show() method returns the button clicked as a DialogResult enumeration. The DialogResult has the values shown in Table 17.3.

Table 17.3. Enumerators for DialogResult

Member

Description

Abort

Return value Abort; usually sent from a button labeled Abort.

Cancel

Return value Cancel; usually sent from a button labeled Cancel.

Ignore

Return value Ignore; usually sent from a button labeled Ignore.

No

Return value No; usually sent from a button labeled No.

None

Nothing is returned from the dialog box. The model dialog continues running.

OK

Return value OK; usually sent from a button labeled OK.

Retry

Return value Retry; usually sent from a button labeled Retry.

Yes

Return value Yes; usually sent from a button labeled Yes.


By the Way

Note the text "usually sent from" in the descriptions of the DialogResult values. When you create custom dialog boxes (as shown later in this hour), you can assign a DialogResult to any button of your choosing.


Performing actions based on the button clicked is a matter of using one of the decision constructs. For example:

if (MessageBox.Show("Would you like to do X?", "MessageBox sample",    MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) {    // Code to do X would go here. }


As you can see, MessageBox.Show() is a method that gives you a lot of bang for your buck; it offers considerable flexibility.

Creating Good Messages

The MessageBox.Show() method is surprisingly simple to use, considering all the different forms of messages it enables you to create. The real trick is in providing appropriate messages to users at appropriate times. In addition to considering the icon and buttons to display in a message, you should follow these guidelines for crafting message text:

  • Use a formal tone. Don't use large words, and avoid using contractions. Strive to make the text immediately understandable and not overly fancy; a message box is not a place to show off your literary skills.

  • Limit messages to two or three lines. Lengthy messages are not only more difficult for users to read, but they can also be intimidating. When a message box is used to ask a question, make the question as succinct as possible.

  • Never make users feel as though they've done something wrong. Users will, and do, make mistakes, but you should craft messages that take the sting out of the situation.

  • Spell check all message text. The Visual C# code editor doesn't spell check for you, so you should type your messages in a program such as Word and spell check the text before pasting it into your code. Spelling errors have an adverse effect on a user's perception of a program.

  • Avoid technical jargon. Just because someone uses software doesn't mean that they are a technical person; explain things in plain English (or whatever the native language of the GUI happens to be).

  • Be sure that the buttons match the text! For example, don't show the Yes/No buttons if the text doesn't present a question to the user.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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