Export (0) Print
Expand All
26 out of 45 rated this helpful - Rate this topic

Displaying Message Boxes

Visual Studio .NET 2003

A MessageBox is a predefined dialog box that displays application-related information to the user. Message boxes are also used to request information from the user.

To display information to the user in a message box

  1. Navigate to where you would like to add the code for the message box.
  2. Add code using the MessageBox.Show method.

    The following code demonstrates how to call the Show method of the MessageBox class to display information to the user. The call to the Show method uses the optional style parameter to specify the type of icon to display in the message box that best fits the type of message box being displayed:

    ' Visual Basic
    Public Sub PerformCalculations()
       ' Code is entered here that performs a calculation.
       ' Display a message box informing the user that the calculations 
       ' are complete.
          MessageBox.Show("The calculations are complete", "My Application", _
               MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
    End Sub
    
    // C#
    public void PerformCalculations() 
    {
       // Code is entered here that performs a calculation
       // Display a message box informing the user that the calculations 
       // are complete
       MessageBox.Show ("The calculations are complete", "My Application", 
    MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
    }
    
    // C++
    public:
       void PerformCalculations()
       {
          // Code is entered here that performs a calculation
          // Display a message box informing the user that the calculations 
          // are complete
          MessageBox::Show(S"The calculations are complete",
             S"My Application", MessageBoxButtons::OKCancel,
             MessageBoxIcon::Asterisk);
       }
    

Message boxes can also receive input. The Show method of the MessageBox class returns a value that can be used to determine a choice made by the user. You can store this value in an integer or compare the value returned when you display the message box using an if statement. The Style parameter of the Show method can be set to display the proper buttons to ask a user for information.

To display a message box to request information

  1. Open the Code Editor for your class and navigate to where you would like to add the code for the message box.
  2. Add code that uses the Show method of the MessageBox class to display a message box.

    The following code demonstrates how to call the MessageBox method to retrieve information from the user and then determine the value that was selected:

    ' Visual Basic
    Public Sub ExitApplication()
       ' Display a message box asking users if they 
       ' want to exit the application.
       If MessageBox.Show ("Do you want to exit?", "My Application", _
             MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
             = DialogResult.Yes Then
          Application.Exit
       End If
    End Sub
    
    // C#
    public void ExitApplication()
    {
       // Display a message box asking users if they
       // want to exit the application.
       if (MessageBox.Show ("Do you want to exit?", "My Application",
             MessageBoxButtons.YesNo, MessageBoxIcon.Question)
             == DialogResult.Yes) 
       {
          Application.Exit();
       }
    }
    
    // C++
    public:
       void ExitApplication()
       {
          // Display a message box asking users if they
          // want to exit the application.
          if (MessageBox::Show(S"Do you want to exit?",
             S"My Application", MessageBoxButtons::YesNo,
             MessageBoxIcon::Question) == DialogResult::Yes)
          {
             Application::Exit();
          }
       }
    
    Visual Basic Note   In Visual Basic, using
    MsgBox()
    to create a message box to display to users is still supported, though the new syntax,
    MessageBox.Show()
    , as seen above, is preferred. Thus, in reference to the previous code example, the following is acceptable in Visual Basic.
    ' Visual Basic
    Public Sub ExitApplication()
       If MsgBox("Do you want to exit?", MsgBoxStyle.Exclamation, _
    "My Application") = MsgBoxResult.Yes Then
         Application.Exit()
       End If
    End Sub
    

    For more information about MsgBox(), see MsgBox Function.

See Also

Dialog Boxes in Windows Forms | MessageBox Class | MsgBox Result Constant Changes in Visual Basic .NET | MsgBox Style Constant Changes in Visual Basic .NET | Form.DialogResult Property | Creating Dialog Boxes | Creating Windows Forms

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.