Using a Message Box


One of the most useful built-in features of Windows is the message box. A message box is a predefined window that lets you display a message. You can also obtain simple responses from the user, such as Yes, No, or OK. In a form-based program, a message box is supported by the MessageBox class. You don’t create an object of that class, however. Instead, to display a message box, call the static method Show( ), which is defined by MessageBox.

The Show( ) method has several forms. The one we will be using is shown here:

 public static DialogResult Show(string msg, string caption,                                 MessageBoxButtons mbb)

The string passed through msg is displayed in the body of the box. The caption of the message box window is passed in caption. The buttons that will be displayed are specified by mbb. The user’s response is returned.

MessageBoxButtons is an enumeration that defines the following values:

AbortRetryIgnore

OK

OKCancel

RetryCancel

YesNo

YesNoCancel

Each of these values describes the buttons that will be included in a message box. For example, if mbb contains YesNo, then the Yes and No buttons are included in the message box.

The value returned by Show( ) indicates which button was pressed. It will be one of these values:

Abort

Cancel

Ignore

No

None

OK

Retry

Yes

Your program can examine the return value to determine the course of action that the user desires. For example, if the message box prompts the user before overwriting a file, your program can prevent the overwrite if the user clicks Cancel, and it can allow the overwrite if the user clicks OK.

The following program adds a stop button and a message box to the preceding example. In the stop button handler, a message box is displayed that asks the user if he/she wants to stop the program. If the user clicks Yes, the program is stopped. If the user clicks No, the program continues running.

 // Add a stop button. using System; using System.Windows.Forms; using System.Drawing; class ButtonForm : Form {   Button MyButton;   Button StopButton;   public ButtonForm() {     Text = "Adding a Stop Button";     // Create the buttons.     MyButton = new Button();     MyButton.Text = "Press Here";     MyButton.Location = new Point(100, 200);     StopButton = new Button();     StopButton.Text = "Stop";     StopButton.Location = new Point(100, 100);     // Add the button event handlers to the window.     MyButton.Click += MyButtonClick;     Controls.Add(MyButton);     StopButton.Click += StopButtonClick;     Controls.Add(StopButton);   }   [STAThread]   public static void Main() {     ButtonForm skel = new ButtonForm();     Application.Run(skel);   }   // Handler for MyButton.   protected void MyButtonClick(object who, EventArgs e) {     if(MyButton.Top == 200)       MyButton.Location = new Point(10, 10);     else       MyButton.Location = new Point(100, 200);   }      // Handler for StopButton.   protected void StopButtonClick(object who, EventArgs e) {     // If user answers Yes, terminate the program.     DialogResult result = MessageBox.Show("Stop Program?",                             "Terminate",                             MessageBoxButtons.YesNo);     if(result == DialogResult.Yes) Application.Exit();   } }

Let’s look closely at how the message box is used. Inside the ButtonForm constructor, a second button is added. This button contains the text “Stop,” and its event handler is linked to StopButtonClick( ).

Inside StopButtonClick( ), the message box is displayed by the following statement:

 // If user answers Yes, terminate the program. DialogResult result = MessageBox.Show("Stop Program?",                         "Terminate",                         MessageBoxButtons.YesNo);

Here, the message inside the box is “Stop Program?”, the caption is “Terminate,” and the buttons to be displayed are Yes and No. When Show( ) returns, the user’s response is assigned to result. That response is then examined by the following code to determine the course of action:

 if(result == DialogResult.Yes) Application.Exit();

If the user clicks the Yes button, the program is stopped by calling Application.Exit( ), which causes the immediate termination of the program. Otherwise, no action is taken and the program continues running.

Sample output is shown in Figure 26-3.

image from book
Figure 26-3: Sample output from the Stop Button program




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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