次の方法で共有


MessageBox.Show メソッド (String, String, MessageBoxButtons, MessageBoxIcon, MessageBoxDefaultButton)

指定したテキスト、キャプション、ボタン、アイコン、および既定のボタンを表示するメッセージ ボックスを表示します。

Overloads Public Shared Function Show( _
   ByVal text As String, _   ByVal caption As String, _   ByVal buttons As MessageBoxButtons, _   ByVal icon As MessageBoxIcon, _   ByVal defaultButton As MessageBoxDefaultButton _) As DialogResult
[C#]
public static DialogResult Show(stringtext,stringcaption,MessageBoxButtonsbuttons,MessageBoxIconicon,MessageBoxDefaultButtondefaultButton);
[C++]
public: static DialogResult Show(String* text,String* caption,MessageBoxButtonsbuttons,MessageBoxIconicon,MessageBoxDefaultButtondefaultButton);
[JScript]
public static function Show(
   text : String,caption : String,buttons : MessageBoxButtons,icon : MessageBoxIcon,defaultButton : MessageBoxDefaultButton) : DialogResult;

パラメータ

  • text
    メッセージ ボックスに表示するテキスト。
  • caption
    メッセージ ボックスのタイトル バーに表示するテキスト。
  • buttons
    メッセージ ボックスに表示されるボタンを指定する MessageBoxButtons 値の 1 つ。
  • icon
    メッセージ ボックスに表示されるアイコンを指定する MessageBoxIcon 値の 1 つ。
  • defaultButton
    メッセージ ボックスの既定のボタンを指定する MessageBoxDefaultButton 値の 1 つ。

戻り値

DialogResult 値の 1 つ。

例外

例外の種類 条件
InvalidEnumArgumentException buttonsMessageBoxButtons のメンバではありません。

または

iconMessageBoxIcon のメンバではありません。

または

defaultButtonMessageBoxDefaultButton のメンバではありません。

InvalidOperationException ユーザー対話モード以外のモードで実行されているプロセスで、 MessageBox を表示しようとしました。このモードは、 SystemInformation.UserInteractive プロパティで指定されています。

解説

メッセージ ボックスには、最大 3 つのボタンを配置できます。

使用例

 
Private Sub ValidateUserEntry3()

    ' 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)

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

        }

    }

}


[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 = 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);

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

.NET Framework セキュリティ:

参照

MessageBox クラス | MessageBox メンバ | System.Windows.Forms 名前空間 | MessageBox.Show オーバーロードの一覧 | MessageBoxButtons | MessageBoxIcon | MessageBoxDefaultButton