次の方法で共有


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

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

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

パラメータ

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

戻り値

DialogResult 値の 1 つ。

例外

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

または

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

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

解説

owner パラメータを使用して、 IWin32Window インターフェイスを実装する特定のオブジェクトを指定し、そのオブジェクトの前にメッセージ ボックスを配置できます。メッセージ ボックスはモーダル ダイアログです。つまり、キーボードやマウスのクリックによる入力は、モーダル フォーム上のオブジェクトに対してしか発生しません。プログラムで別のフォームに対する入力を行うためには、通常はユーザーの操作に応答する形で、事前にモーダル フォームを非表示にするか、閉じる必要があります。

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

使用例

 
Private Sub ValidateUserEntry4()

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

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

        }

    }

}

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

            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 Framework セキュリティ:

参照

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