MessageBox クラス

定義

ユーザーにメッセージを表示するメッセージ ウィンドウ (ダイアログ ボックスとも呼ばれます) を表示します。 これはモーダル ウィンドウであり、ユーザーが閉じるまでアプリケーション内の他のアクションをブロックします。 MessageBoxには、ユーザーに通知して指示するテキスト、ボタン、シンボルを含めることができます。

public ref class MessageBox
public class MessageBox
type MessageBox = class
Public Class MessageBox
継承
MessageBox

次のコード例は、 MessageBox を使用して、 TextBoxに不足しているエントリをユーザーに通知する方法を示しています。 この例では、ServerName という名前のTextBoxを持つ既存のフォームからメソッドを呼び出す必要があります。

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 = "No Server Name Specified";
         MessageBoxButtons buttons = MessageBoxButtons::YesNo;
         System::Windows::Forms::DialogResult result;

         // Displays the MessageBox.
         result = MessageBox::Show( this, message, caption, buttons );
         if ( result == ::DialogResult::Yes )
         {
            // Closes the parent form.
            this->Close();
         }
      }
   }
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();
        }
    }
}
Private Sub ValidateUserEntry()
    ' 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 = "Error Detected in Input"
        Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo

        Dim Result As DialogResult

        'Displays the MessageBox
        Result = MessageBox.Show(Message, Caption, Buttons)

        ' Gets the result of the MessageBox display.
        If Result = System.Windows.Forms.DialogResult.Yes Then
            ' Closes the parent form.
            Me.Close()
        End If
    End If
End Sub

次のコード例は、ユーザーに "はい" または "いいえ" の質問をし、応答に基づいて決定を下す方法を示しています。

private:
   void Form1_FormClosing(Object^ sender, FormClosingEventArgs^ e)
   {
      // If the no button was pressed ...
      if ((MessageBox::Show(
         "Are you sure that you would like to close the form?", 
         "Form Closing", MessageBoxButtons::YesNo, 
         MessageBoxIcon::Question) == DialogResult::No))
      {
         // cancel the closure of the form.
         e->Cancel = true;
      }
   }
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    const string message =
        "Are you sure that you would like to close the form?";
    const string caption = "Form Closing";
    var result = MessageBox.Show(message, caption,
                                 MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question);

    // If the no button was pressed ...
    if (result == DialogResult.No)
    {
        // cancel the closure of the form.
        e.Cancel = true;
    }
}
Private Sub Form1_FormClosing( _
    ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.FormClosingEventArgs) _
    Handles MyBase.FormClosing

    Dim message As String = _
            "Are you sure that you would like to close the form?"
    Dim caption As String = "Form Closing"
    Dim result = MessageBox.Show(message, caption, _
                                 MessageBoxButtons.YesNo, _
                                 MessageBoxIcon.Question)

    ' If the no button was pressed ...
    If (result = DialogResult.No) Then
        ' cancel the closure of the form.
        e.Cancel = True
    End If
End Sub

注釈

MessageBox クラスの新しいインスタンスを作成することはできません。 メッセージ ボックスを表示するには、 static メソッド MessageBox.Showを呼び出します。 メッセージ ボックスに表示されるタイトル、メッセージ、ボタン、アイコンは、このメソッドに渡すパラメーターによって決まります。

メソッド

名前 説明
Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Objectの簡易コピーを作成します。

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

指定したヘルプ ファイル、 HelpNavigator、ヘルプ トピックを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、ヘルプ ボタンを含むメッセージ ボックスを表示します。

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

指定したヘルプ ファイルと HelpNavigatorを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、ヘルプ ボタンを含むメッセージ ボックスを表示します。

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

指定したヘルプ ファイルとヘルプ キーワードを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、およびヘルプ ボタンを含むメッセージ ボックスを表示します。

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

指定したヘルプ ファイルを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、およびヘルプ ボタンを含むメッセージ ボックスを表示します。

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

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

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

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

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

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

Show(IWin32Window, String, String, MessageBoxButtons)

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

Show(IWin32Window, String, String)

指定したオブジェクトの前に、指定したテキストとキャプションを含むメッセージ ボックスを表示します。

Show(IWin32Window, String)

指定したオブジェクトの前に、指定したテキストを含むメッセージ ボックスを表示します。

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

指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、ヘルプ ボタンを含むメッセージ ボックスを表示します。

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

指定したヘルプ ファイル、 HelpNavigator、ヘルプ トピックを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、ヘルプ ボタンを含むメッセージ ボックスを表示します。

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

指定したヘルプ ファイルと HelpNavigatorを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、ヘルプ ボタンを含むメッセージ ボックスを表示します。

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

指定したヘルプ ファイルとヘルプ キーワードを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、およびヘルプ ボタンを含むメッセージ ボックスを表示します。

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

指定したヘルプ ファイルを使用して、指定したテキスト、キャプション、ボタン、アイコン、既定のボタン、オプション、およびヘルプ ボタンを含むメッセージ ボックスを表示します。

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

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

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

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

Show(String, String, MessageBoxButtons, MessageBoxIcon)

指定したテキスト、キャプション、ボタン、アイコンを含むメッセージ ボックスを表示します。

Show(String, String, MessageBoxButtons)

指定したテキスト、キャプション、ボタンを含むメッセージ ボックスを表示します。

Show(String, String)

指定したテキストとキャプションを含むメッセージ ボックスを表示します。

Show(String)

指定したテキストを含むメッセージ ボックスを表示します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

適用対象

こちらもご覧ください