How to: Display Message Boxes

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 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:

    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
    
    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);
    }
    
    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);
       }
    

    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:

    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
    
    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();
       }
    }
    
    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();
          }
       }
    
    Visual Basic noteVisual 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.

    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

Tasks

How to: Create Dialog Boxes at Design Time

Reference

MessageBox

MsgBox Result Constants for Visual Basic 6.0 Users

MsgBox Style Constants for Visual Basic 6.0 Users

Form.DialogResult

Other Resources

Dialog Boxes in Windows Forms

Creating a New Windows Form