Export (0) Print
Expand All

MessageBox.Show Method

Displays a message box.

Overload List

Displays a message box with specified text.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Show(String) As DialogResult
[C#] public static DialogResult Show(string);
[C++] public: static DialogResult Show(String*);
[JScript] public static function Show(String) : DialogResult;

Displays a message box in front of the specified object and with the specified text.

[Visual Basic] Overloads Public Shared Function Show(IWin32Window, String) As DialogResult
[C#] public static DialogResult Show(IWin32Window, string);
[C++] public: static DialogResult Show(IWin32Window*, String*);
[JScript] public static function Show(IWin32Window, String) : DialogResult;

Displays a message box with specified text and caption.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Show(String, String) As DialogResult
[C#] public static DialogResult Show(string, string);
[C++] public: static DialogResult Show(String*, String*);
[JScript] public static function Show(String, String) : DialogResult;

Displays a message box in front of the specified object and with the specified text and caption.

[Visual Basic] Overloads Public Shared Function Show(IWin32Window, String, String) As DialogResult
[C#] public static DialogResult Show(IWin32Window, string, string);
[C++] public: static DialogResult Show(IWin32Window*, String*, String*);
[JScript] public static function Show(IWin32Window, String, String) : DialogResult;

Displays a message box with specified text, caption, and buttons.

[Visual Basic] Overloads Public Shared Function Show(String, String, MessageBoxButtons) As DialogResult
[C#] public static DialogResult Show(string, string, MessageBoxButtons);
[C++] public: static DialogResult Show(String*, String*, MessageBoxButtons);
[JScript] public static function Show(String, String, MessageBoxButtons) : DialogResult;

Displays a message box in front of the specified object and with the specified text, caption, and buttons.

[Visual Basic] Overloads Public Shared Function Show(IWin32Window, String, String, MessageBoxButtons) As DialogResult
[C#] public static DialogResult Show(IWin32Window, string, string, MessageBoxButtons);
[C++] public: static DialogResult Show(IWin32Window*, String*, String*, MessageBoxButtons);
[JScript] public static function Show(IWin32Window, String, String, MessageBoxButtons) : DialogResult;

Displays a message box with specified text, caption, buttons, and icon.

[Visual Basic] Overloads Public Shared Function Show(String, String, MessageBoxButtons, MessageBoxIcon) As DialogResult
[C#] public static DialogResult Show(string, string, MessageBoxButtons, MessageBoxIcon);
[C++] public: static DialogResult Show(String*, String*, MessageBoxButtons, MessageBoxIcon);
[JScript] public static function Show(String, String, MessageBoxButtons, MessageBoxIcon) : DialogResult;

Displays a message box in front of the specified object and with the specified text, caption, buttons, and icon.

[Visual Basic] Overloads Public Shared Function Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon) As DialogResult
[C#] public static DialogResult Show(IWin32Window, string, string, MessageBoxButtons, MessageBoxIcon);
[C++] public: static DialogResult Show(IWin32Window*, String*, String*, MessageBoxButtons, MessageBoxIcon);
[JScript] public static function Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon) : DialogResult;

Displays a message box with the specified text, caption, buttons, icon, and default button.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Shared Function Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) As DialogResult
[C#] public static DialogResult Show(string, string, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton);
[C++] public: static DialogResult Show(String*, String*, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton);
[JScript] public static function Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) : DialogResult;

Displays a message box in front of the specified object and with the specified text, caption, buttons, icon, and default button.

[Visual Basic] Overloads Public Shared Function Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) As DialogResult
[C#] public static DialogResult Show(IWin32Window, string, string, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton);
[C++] public: static DialogResult Show(IWin32Window*, String*, String*, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton);
[JScript] public static function Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton) : DialogResult;

Displays a message box with the specified text, caption, buttons, icon, default button, and options.

[Visual Basic] Overloads Public Shared Function Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions) As DialogResult
[C#] public static DialogResult Show(string, string, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions);
[C++] public: static DialogResult Show(String*, String*, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions);
[JScript] public static function Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions) : DialogResult;

Displays a message box in front of the specified object and with the specified text, caption, buttons, icon, default button, and options.

[Visual Basic] Overloads Public Shared Function Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions) As DialogResult
[C#] public static DialogResult Show(IWin32Window, string, string, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions);
[C++] public: static DialogResult Show(IWin32Window*, String*, String*, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions);
[JScript] public static function Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions) : DialogResult;

Example

[Visual Basic, C#, C++] The following example demonstrates how to display a MessageBox with the options supported by this overload of Show. After verifying that a string variable, ServerName, is empty, the example displays a MessageBox, offering the user the option to cancel the operation. If the Show method's return value evaluates to Yes, the form that displayed the MessageBox is closed.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Show. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Private Sub ValidateUserEntry2()


    ' Checks the value of the text.

    If ServerName.Text.Length = 0 Then

        ' Initializes variables to pass to the MessageBox.Show method.

        Dim Message As String = "You did not enter a server name. Cancel this operation?"
        Dim Caption As String = "No Server Name Specified"
        Dim Buttons As Integer = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays a MessageBox using the Question icon and specifying the No button as the default.

        Result = MessageBox.Show(Me, Message, Caption, MessageBoxButtons.YesNo, _
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign)


        ' Gets the result of the MessageBox display.

        If Result = DialogResult.Yes Then

            ' Closes the parent form.

            Me.Close()

        End If

    End If

End Sub

[C#] 

    private void validateUserEntry2()
    {

        // Checks the value of the text.

        if(serverName.Text.Length == 0)
        {

            // Initializes the variables to pass to the MessageBox.Show method.

            string message = "You did not enter a server name. Cancel this operation?";
            string caption = "No Server Name Specified";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result;

            // Displays the MessageBox.

            result = MessageBox.Show(this, message, caption, buttons,
                MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, 
                MessageBoxOptions.RightAlign);

            if(result == DialogResult.Yes)
            {

                // Closes the parent form.

                this.Close();

            }

        }

    }


[C++] 
private:
    void validateUserEntry2() {
        // Checks the value of the text.
        if (serverName->Text->Length == 0) {

            // Initializes the variables to pass to the MessageBox::Show method.
            String* message = S"You did not enter a server name. Cancel this operation?";
            String* caption = S"No Server Name Specified";
            MessageBoxButtons buttons = MessageBoxButtons::YesNo;
            System::Windows::Forms::DialogResult result;

            // Displays the MessageBox.
            result = MessageBox::Show(this, message, caption, buttons,
                MessageBoxIcon::Question, MessageBoxDefaultButton::Button1, 
                MessageBoxOptions::RightAlign);

            if (result == DialogResult::Yes) {
                // Closes the parent form.
                this->Close();
            }
        }
    }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

MessageBox Class | MessageBox Members | System.Windows.Forms Namespace

Show:
© 2014 Microsoft