Export (0) Print
Expand All
Expand Minimize

MessageBox.Show Method (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton, MessageBoxOptions, Boolean)

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public static DialogResult Show(
	string text,
	string caption,
	MessageBoxButtons buttons,
	MessageBoxIcon icon,
	MessageBoxDefaultButton defaultButton,
	MessageBoxOptions options,
	bool displayHelpButton
)

Parameters

text
Type: System.String

The text to display in the message box.

caption
Type: System.String

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

buttons
Type: System.Windows.Forms.MessageBoxButtons

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

icon
Type: System.Windows.Forms.MessageBoxIcon

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

defaultButton
Type: System.Windows.Forms.MessageBoxDefaultButton

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

options
Type: System.Windows.Forms.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
Type: System.Boolean

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

Return Value

Type: System.Windows.Forms.DialogResult
One of the DialogResult values.

ExceptionCondition
InvalidEnumArgumentException

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.

InvalidOperationException

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

ArgumentException

options specified both DefaultDesktopOnly and ServiceNotification.

-or-

buttons specified an invalid combination of MessageBoxButtons.

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.

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.

using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;


...


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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft