MessageBox.Show Method

Definition

Displays a message box.

Overloads

Show(String)

Displays a message box with specified text.

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String)

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

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object)

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file, HelpNavigator, and Help topic.

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String)

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

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator)

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

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String)

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

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)

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

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String)

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

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, Boolean)

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

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator)

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

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

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

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon)

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

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

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

Show(IWin32Window, String, String, MessageBoxButtons)

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

Show(String, String, MessageBoxButtons, MessageBoxIcon)

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

Show(IWin32Window, String, String)

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

Show(String, String, MessageBoxButtons)

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

Show(IWin32Window, String)

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

Show(String, String)

Displays a message box with specified text and caption.

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)

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

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object)

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file, HelpNavigator, and Help topic.

Show(String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

Displays a message box with specified text.

C#
public static System.Windows.Forms.DialogResult Show(string text);
C#
public static System.Windows.Forms.DialogResult Show(string? text);

Parameters

text
String

The text to display in the message box.

Returns

One of the DialogResult values.

Examples

The following code example displays a simple message box.

C#
private void DisplayMessageBoxText()
{
         MessageBox.Show("Hello, world.");
}

Remarks

By default, the message box displays an OK button. The message box does not contain a caption in the title.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

keyword
String

The Help keyword to display when the user clicks the Help button.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box parented to the main window. The message box displays a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened and the topic identified by the mspaint.chm::/paint_brush.htm keyword is displayed. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file, 
// and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
// associated topic.
DialogResult r8 = MessageBox.Show (this, "Message with Help file and keyword.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 0, 
                                   "mspaint.chm", 
                                   "mspaint.chm::/paint_brush.htm");

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur. You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level owner.

When the user clicks the Help button, the Help file specified in the helpFilePath parameter is opened and the Help keyword topic identified by the keyword parameter is displayed. The form that owns the message box (or the active form) also receives the HelpRequested event.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file, HelpNavigator, and Help topic.

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object param);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object? param);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

navigator
HelpNavigator

One of the HelpNavigator values.

param
Object

The numeric ID of the Help topic to display when the user clicks the Help button.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box with a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened and the Help index tab and the topic identified by the ovals keyword are displayed. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display a message box. The Help button opens the Mspaint.chm Help file, 
// shows index with the "ovals" keyword selected, and displays the
// associated topic.
DialogResult r5 = MessageBox.Show ("Message with Help file and Help navigator with additional parameter.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.KeywordIndex, "ovals");

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

When the user clicks the Help button, the Help file specified in the helpFilePath parameter is opened and the Help content identified by the navigator parameter is displayed. The form that owns the message box (or the active form) also receives the HelpRequested event.

Compiled help files provide table of contents, index, search, and keyword links in pages. You can use the following values for navigator: TableOfContents, Find, Index, or Topic.

You can use param to provide further refinement of the Topic command. If the value specified in the navigator parameter is TableOfContents, Index, or Find, this value should be null. If the navigator parameter references Topic, this value should reference an object that contains the numeric value of the topic to display.

The helpFilePath parameter can be of the form C:\path\sample.chm or /folder/file.htm.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box that parented to the main form. The message box displays a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display a message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file.
DialogResult r2 = MessageBox.Show (this, "Message with Help file.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, 
                                   "mspaint.chm");

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur. You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner.

When the user clicks the Help button, the Help file specified in the helpFilePath parameter is opened. The form that owns the message box (or the active form) also receives the HelpRequested event.

The helpFilePath parameter can be of the form C:\path\sample.chm or /folder/file.htm.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

navigator
HelpNavigator

One of the HelpNavigator values.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box with a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened and the Help index tab is displayed. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display a message box. The Help button opens 
// the Mspaint.chm Help file and shows the Help contents 
// on the Index tab.
DialogResult r3 = MessageBox.Show ("Message with Help file and Help navigator.", 
                                   "Help Caption", MessageBoxButtons.OK,
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.Index);

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

When the user clicks the Help button, the help file specified in the helpFilePath parameter is opened and the Help content identified by the navigator parameter is displayed. The form that owns the message box (or the active form) also receives the HelpRequested event.

Compiled Help files provide table of contents, index, search, and keyword links in pages. You can use the following values for navigator: TableOfContents, Find, Index, or Topic.

The helpFilePath parameter can be of the form C:\path\sample.chm or /folder/file.htm.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, string keyword);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

keyword
String

The Help keyword to display when the user clicks the Help button.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box with a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened and the topic identified by the mspaint.chm::/paint_brush.htm keyword is displayed. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display a message box. The Help button opens the Mspaint.chm Help file, 
// and the "mspaint.chm::/paint_brush.htm" Help keyword shows the 
// associated topic.
DialogResult r7 = MessageBox.Show ("Message with Help file and keyword.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 0, 
                                   "mspaint.chm", 
                                   "mspaint.chm::/paint_brush.htm");

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

When the user clicks the Help button, the Help file specified in the helpFilePath parameter is opened and the Help keyword topic identified by the keyword parameter is displayed. The form that owns the message box (or the active form) also receives the HelpRequested event.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values the specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

defaultButton is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

options specified DefaultDesktopOnly or ServiceNotification and specified a value in the owner parameter. These two options should be used only if you invoke the version of this method that does not take an owner parameter.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code 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.

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

Remarks

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner. A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box with a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display a message box with a help button. 
// The Help button opens the Mspaint.chm Help file.
DialogResult r1 = MessageBox.Show ("Message with Help file.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, 
                                   "mspaint.chm");

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

When the user clicks Help button, the Help file specified in the helpFilePath parameter is opened. The form that owns the message box (or the active form) also receives the HelpRequested event.

The helpFilePath parameter can be of the form C:\path\sample.chm or /folder/file.htm.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, Boolean)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, bool displayHelpButton);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, bool displayHelpButton);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

displayHelpButton
Boolean

true to show the Help button; otherwise, false. The default is false.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box with a Help button, handle the Control.HelpRequested event, and show a custom help window in response to the event.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display a message box with a Help button. Show a custom Help window
// by handling the HelpRequested event.
private DialogResult AlertMessageWithCustomHelpWindow ()
{
    // Handle the HelpRequested event for the following message.
    this.HelpRequested += new System.Windows.Forms.HelpEventHandler (this.Form1_HelpRequested);

    this.Tag = "Message with Help button.";

    // Show a message box with OK and Help buttons.
    DialogResult r = MessageBox.Show ("Message with Help button.", 
                                      "Help Caption", MessageBoxButtons.OK, 
                                      MessageBoxIcon.Question, 
                                      MessageBoxDefaultButton.Button1, 
                                      0, true);

    // Remove the HelpRequested event handler to keep the event
    // from being handled for other message boxes.
    this.HelpRequested -= new System.Windows.Forms.HelpEventHandler (this.Form1_HelpRequested);

    // Return the dialog box result.
    return r;
}

private void Form1_HelpRequested (System.Object sender, System.Windows.Forms.HelpEventArgs hlpevent)
{
    // Create a custom Help window in response to the HelpRequested event.
    Form helpForm = new Form ();

    // Set up the form position, size, and title caption.
    helpForm.StartPosition = FormStartPosition.Manual;
    helpForm.Size = new Size (200, 400);
    helpForm.DesktopLocation = new Point (this.DesktopBounds.X + 
                                          this.Size.Width, 
                                          this.DesktopBounds.Top);
    helpForm.Text = "Help Form";

    // Create a label to contain the Help text.
    Label helpLabel = new Label ();

    // Add the label to the form and set its text.
    helpForm.Controls.Add (helpLabel);
    helpLabel.Dock = DockStyle.Fill;

    // Use the sender parameter to identify the context of the Help request.
    // The parameter must be cast to the Control type to get the Tag property.
    Control senderControl = sender as Control;

    helpLabel.Text = "Help information shown in response to user action on the '" + 
                      (string)senderControl.Tag + "' message.";

    // Set the Help form to be owned by the main form. This helps
    // to ensure that the Help form is disposed of.
    this.AddOwnedForm (helpForm);

    // Show the custom Help window.
    helpForm.Show ();

    // Indicate that the HelpRequested event is handled.
    hlpevent.Handled = true;
}

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

When the user clicks the Help button, the form that owns the message box (or the active form) receives the HelpRequested event. Handle the HelpRequested event to perform custom Help actions.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

navigator
HelpNavigator

One of the HelpNavigator values.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box parented to the main window. The message box displays a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened and the Help index tab is displayed. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file
// and shows the Help contents on the Index tab.
DialogResult r4 = MessageBox.Show (this, 
                                   "Message with Help file and Help navigator.", 
                                   "Help Caption", MessageBoxButtons.OK,
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.Index);

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur. You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner.

When the user clicks the Help button, the Help file specified in the helpFilePath parameter is opened and the Help content identified by the navigator parameter is displayed. The form that owns the message box (or the active form) also receives the HelpRequested event.

Compiled Help files provide table of contents, index, search, and keyword links in pages. You can use the following values for navigator: TableOfContents, Find, Index, or Topic.

The helpFilePath parameter can be of the form C:\path\sample.chm or /folder/file.htm.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

defaultButton is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

Examples

The following code 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.

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

Remarks

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner. A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

Examples

The following code 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.

C#
private void validateUserEntry4()
{

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

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}

Remarks

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner. A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

defaultButton is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

Examples

The following code 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 with a question box icon, 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.

C#
private void validateUserEntry3()
{

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

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}

Remarks

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

Examples

The following code 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.

C#
private void validateUserEntry5()
{

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

        if(result == DialogResult.Yes)
        {

            // Closes the parent form.

            this.Close();
        }
    }
}

Remarks

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner. A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

Returns

One of the DialogResult values.

Exceptions

The buttons parameter specified is not a member of MessageBoxButtons.

-or-

The icon parameter specified is not a member of MessageBoxIcon.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

Examples

The following code example demonstrates one of the Show methods when handling the ComboBox.DropDown event. To run the example, paste the following code in a form and call the InitializeComboBox method from the form's constructor or Load method.

C#

// Declare ComboBox1.
internal System.Windows.Forms.ComboBox ComboBox1;

// Initialize ComboBox1.
private void InitializeComboBox()
{
    this.ComboBox1 = new ComboBox();
    this.ComboBox1.Location = new System.Drawing.Point(128, 48);
    this.ComboBox1.Name = "ComboBox1";
    this.ComboBox1.Size = new System.Drawing.Size(100, 21);
    this.ComboBox1.TabIndex = 0;
    this.ComboBox1.Text	= "Typical";
    string[] installs = new string[]{"Typical", "Compact", "Custom"};
    ComboBox1.Items.AddRange(installs);
    this.Controls.Add(this.ComboBox1);
    
    // Hook up the event handler.
    this.ComboBox1.DropDown +=  
        new System.EventHandler(ComboBox1_DropDown);
}

// Handles the ComboBox1 DropDown event. If the user expands the  
// drop-down box, a message box will appear, recommending the
// typical installation.
private void ComboBox1_DropDown(object sender, System.EventArgs e)
{
    MessageBox.Show("Typical installation is strongly recommended.", 
    "Install information", MessageBoxButtons.OK, 
        MessageBoxIcon.Information);
}

Remarks

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

Returns

One of the DialogResult values.

Remarks

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner. A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

By default, the message box displays an OK button.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

Returns

One of the DialogResult values.

Exceptions

The buttons parameter specified is not a member of MessageBoxButtons.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

Examples

The following code 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.

C#
private void validateUserEntry()
{
    // 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 = "Error Detected in Input";
        MessageBoxButtons buttons = MessageBoxButtons.YesNo;
        DialogResult result;

        // Displays the MessageBox.
        result = MessageBox.Show(message, caption, buttons);
        if (result == System.Windows.Forms.DialogResult.Yes)
        {
            // Closes the parent form.
            this.Close();
        }
    }
}

Remarks

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

Returns

One of the DialogResult values.

Remarks

You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner. A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur.

By default, the message box displays an OK button. The message box does not contain a caption in the title.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

Displays a message box with specified text and caption.

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

Returns

One of the DialogResult values.

Remarks

By default, the message box displays an OK button.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

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

C#
public static System.Windows.Forms.DialogResult Show(string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);
C#
public static System.Windows.Forms.DialogResult Show(string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options);

Parameters

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code 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 with a question box icon, offering the user the option to cancel the operation. The example uses the RightAlign member of the MessageBoxOptions enumeration to align the text to the right edge of the dialog box. If the Show method's return value evaluates to Yes, the form that displayed the MessageBox is closed.

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

Remarks

You can have a maximum of three buttons on the message box.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Show(IWin32Window, String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, String, HelpNavigator, Object)

Source:
MessageBox.cs
Source:
MessageBox.cs
Source:
MessageBox.cs

Displays a message box with the specified text, caption, buttons, icon, default button, options, and Help button, using the specified Help file, HelpNavigator, and Help topic.

C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window owner, string text, string caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object param);
C#
public static System.Windows.Forms.DialogResult Show(System.Windows.Forms.IWin32Window? owner, string? text, string? caption, System.Windows.Forms.MessageBoxButtons buttons, System.Windows.Forms.MessageBoxIcon icon, System.Windows.Forms.MessageBoxDefaultButton defaultButton, System.Windows.Forms.MessageBoxOptions options, string helpFilePath, System.Windows.Forms.HelpNavigator navigator, object? param);

Parameters

owner
IWin32Window

An implementation of IWin32Window that will own the modal dialog box.

text
String

The text to display in the message box.

caption
String

The text to display in the title bar of the message box.

buttons
MessageBoxButtons

One of the MessageBoxButtons values that specifies which buttons to display in the message box.

icon
MessageBoxIcon

One of the MessageBoxIcon values that specifies which icon to display in the message box.

defaultButton
MessageBoxDefaultButton

One of the MessageBoxDefaultButton values that specifies the default button for the message box.

options
MessageBoxOptions

One of the MessageBoxOptions values that specifies which display and association options will be used for the message box. You may pass in 0 if you wish to use the defaults.

helpFilePath
String

The path and name of the Help file to display when the user clicks the Help button.

navigator
HelpNavigator

One of the HelpNavigator values.

param
Object

The numeric ID of the Help topic to display when the user clicks the Help button.

Returns

One of the DialogResult values.

Exceptions

buttons is not a member of MessageBoxButtons.

-or-

icon is not a member of MessageBoxIcon.

-or-

The defaultButton specified is not a member of MessageBoxDefaultButton.

An attempt was made to display the MessageBox in a process that is not running in User Interactive mode. This is specified by the UserInteractive property.

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

Examples

The following code example demonstrates how to show a message box parented to the main window. The message box displays a Help button. When the user clicks the Help button, the Mspaint.chm Help file is opened and the Help index tab and the topic identified by the ovals keyword are displayed. The example requires that the Mspaint.chm Help file is installed.

C#
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
C#
// Display message box parented to the main form. 
// The Help button opens the Mspaint.chm Help file, 
// shows index with the "ovals" keyword selected, and displays the
// associated topic.
DialogResult r6 = MessageBox.Show (this, 
                                   "Message with Help file and Help navigator with additional parameter.", 
                                   "Help Caption", MessageBoxButtons.OK, 
                                   MessageBoxIcon.Question, 
                                   MessageBoxDefaultButton.Button1, 
                                   0, "mspaint.chm", 
                                   HelpNavigator.KeywordIndex, "ovals");

Remarks

A message box is a modal dialog box, which means no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (typically in response to some user action) before input to another form can occur. You can use the owner parameter to specify a particular object, which implements the IWin32Window interface, that will serve as the dialog box's top-level window and owner.

When the user clicks the Help button, the Help file specified in the helpFilePath parameter is opened and the Help content identified by the navigator parameter is displayed. The form that owns the message box (or the active form) also receives the HelpRequested event.

Compiled help files provide table of contents, index, search, and keyword links in pages. You can use the following values for navigator: TableOfContents, Find, Index, or Topic.

You can use param to provide further refinement of the Topic command. If the value specified in the navigator parameter is TableOfContents, Index, or Find, this value should be null. If the navigator parameter references Topic, this value should reference an object that contains the numeric value of the topic to display.

The helpFilePath parameter can be of the form C:\path\sample.chm or /folder/file.htm.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9