Displaying Messages Using the MessageBox.Show() MethodA 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 IconUsing 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.
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.
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 ClickedYou'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.
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 MessagesThe 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:
|