The Show method is overloaded so that you can optionally show a MessageBox icon, show a title, change the buttons displayed, and set the default button. The only required setting is the text message to be displayed on the form. Figure 11-10 shows the MessageBox displayed by the previous code. Figure 11-10. A basic MessageBox The following code calls the Show method using some of the other parameters. Figure 11-11 shows the resulting MessageBox that gets displayed. For more information on the various parameters and settings available, look up the MessageBox class in the Visual Studio help file. MessageBox.Show("Are you sure you want to quit?", "Closing Application",MessageBoxButton.OKCancel, MessageBoxImage.Question); Figure 11-11. A more complex Messagebox You will often use a MessageBox to query for a user response to a question. The user responds by clicking a button. The result is passed back as the return value of the MessageBox.Show method in the form of a MessageBoxResult enumeration. The following code captures the dialog box result entered by a user and closes the window (or not) depending on the result: MessageBoxResult result = MessageBox.Show("Are you sure you want to quit?", "Closing Application",MessageBoxButton.OKCancel, MessageBoxImage.Question); if (result == MessageBoxResult.OK) { this.Close(); }
The Show method is overloaded so that you can optionally show a MessageBox icon, show a title, change the buttons displayed, and set the default button. The only required setting is the text message to be displayed on the form. Figure 11-10 shows the MessageBox displayed by the previous code. Figure 11-10. A basic MessageBox The following code calls the Show method using some of the other parameters. Figure 11-11 shows the resulting MessageBox that gets displayed. For more information on the various parameters and settings available, look up the MessageBox class in the Visual Studio help file. MessageBox.Show("Are you sure you want to quit?", "Closing Application",MessageBoxButton.OKCancel, MessageBoxImage.Question); Figure 11-11. A more complex Messagebox You will often use a MessageBox to query for a user response to a question. The user responds by clicking a button. The result is passed back as the return value of the MessageBox.Show method in the form of a MessageBoxResult enumeration. The following code captures the dialog box result entered by a user and closes the window (or not) depending on the result: MessageBoxResult result = MessageBox.Show("Are you sure you want to quit?", "Closing Application",MessageBoxButton.OKCancel, MessageBoxImage.Question); if (result == MessageBoxResult.OK) { this.Close(); }
68793928390368.docx