メッセージ ボックスを開く方法 (WPF .NET)

"メッセージ ボックス" は、情報を迅速に表示するために使用し、必要に応じてユーザーが意思決定できるようにするためのダイアログ ボックスです。 メッセージ ボックスへのアクセスは、MessageBox クラスによって実現されます。 メッセージ ボックスは" モーダル" に表示されます。 また、メッセージ ボックスを表示するコードは、ユーザーが閉じるボタンまたは応答ボタンを使用してメッセージ ボックスを閉じるまで停止します。

次の図は、メッセージ ボックスの各部分を示しています。

A figure that shows the parts of a message box for WPF.

  • キャプション付きのタイトル バー (1)。
  • 閉じるボタン (2)。
  • アイコン (3)。
  • ユーザーに表示されるメッセージ (4)。
  • 応答ボタン (5)。

複雑なデータを表示または収集する場合は、メッセージ ボックスよりもダイアログ ボックスが適している可能性があります。 詳細については、「ダイアログ ボックスの概要」を参照してください。

メッセージ ボックスを表示する

メッセージ ボックスを作成するには、MessageBox クラスを使用します。 MessageBox.Show メソッドを使用すると、次のコードに示すように、メッセージ ボックスのテキスト、タイトル、アイコン、およびボタンを構成できます。

string messageBoxText = "Do you want to save changes?";
string caption = "Word Processor";
MessageBoxButton button = MessageBoxButton.YesNoCancel;
MessageBoxImage icon = MessageBoxImage.Warning;
MessageBoxResult result;

result = MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.Yes);
Dim messageBoxText As String = "Do you want to save changes?"
Dim caption As String = "Word Processor"
Dim Button As MessageBoxButton = MessageBoxButton.YesNoCancel
Dim Icon As MessageBoxImage = MessageBoxImage.Warning
Dim result As MessageBoxResult

result = MessageBox.Show(messageBoxText, caption, Button, Icon, MessageBoxResult.Yes)

MessageBox.Show メソッドのオーバーロードにより、メッセージ ボックスを構成する方法が提供されます。 設定できるオプションは次のとおりです。

  • タイトル バーのキャプション
  • メッセージ アイコン
  • メッセージ テキスト
  • 応答ボタン

次に、メッセージ ボックスの使用例をいくつか示します。

  • アラートを表示する。

    MessageBox.Show("Unable to save file, try again.");
    
    MessageBox.Show("Unable to save file, try again.")
    

    さきほどのコードでは、次の画像のようなメッセージ ボックスが表示されます。

    A simple message box for WPF that has no options configured.

    メッセージ ボックス クラスによって提供されるオプションを使用することをお勧めします。 以前と同じアラートを使用して、より視覚に訴えるオプションを設定します。

    MessageBox.Show("Unable to save file, try again.", "Save error", MessageBoxButton.OK, MessageBoxImage.Error);
    
    MessageBox.Show("Unable to save file, try again.", "Save error", MessageBoxButton.OK, MessageBoxImage.Error)
    

    さきほどのコードでは、次の画像のようなメッセージ ボックスが表示されます。

    A warning message box for WPF that has an icon, caption, and text.

  • 警告を表示する。

    MessageBox.Show("If you close the next window without saving, your changes will be lost.", "Configuration", MessageBoxButton.OK, MessageBoxImage.Warning);
    
    MessageBox.Show("If you close the next window without saving, your changes will be lost.", "Configuration", MessageBoxButton.OK, MessageBoxImage.Warning)
    

    さきほどのコードでは、次の画像のようなメッセージ ボックスが表示されます。

    A simple message box for WPF that has displays a warning icon.

  • ユーザーに質問をする。

    if (MessageBox.Show("If the file save fails, do you want to automatically try again?",
                        "Save file",
                        MessageBoxButton.YesNo,
                        MessageBoxImage.Question) == MessageBoxResult.Yes)
    {
        // Do something here
    }
    
    If MessageBox.Show("If the file save fails, do you want to automatically try again?",
                       "Save file",
                       MessageBoxButton.YesNo,
                       MessageBoxImage.Question) = MessageBoxResult.Yes Then
    
        ' Do something here
    
    End If
    

    さきほどのコードでは、次の画像のようなメッセージ ボックスが表示されます。

    A simple message box for WPF that prompts the user with a yes or no question.

メッセージ ボックスの応答を処理する

MessageBox.Show メソッドにより、メッセージ ボックスが表示され、結果が返されます。 結果には、ユーザーがどのようにメッセージ ボックスを閉じたかが示されます。

result = MessageBox.Show(messageBoxText, caption, button, icon, MessageBoxResult.Yes);

switch (result)
{
    case MessageBoxResult.Cancel:
        // User pressed Cancel
        break;
    case MessageBoxResult.Yes:
        // User pressed Yes
        break;
    case MessageBoxResult.No:
        // User pressed No
        break;
}
result = MessageBox.Show(messageBoxText, caption, Button, Icon, MessageBoxResult.Yes)

Select Case result
    Case MessageBoxResult.Cancel
        ' User pressed Cancel
    Case MessageBoxResult.Yes
        ' User pressed Yes
    Case MessageBoxResult.No
        ' User pressed No
End Select

ユーザーがメッセージ ボックスの下部にあるボタンを押すと、対応する MessageBoxResult が返されます。 ただし、ユーザーが ESC キーを押すか、 [閉じる] ボタン (メッセージ ボックスの図の #2) を押した場合、メッセージ ボックスの結果はボタンのオプションによって異なります。

ボタンのオプション ESC キーまたは [閉じる] ボタンの結果
OK OK
OKCancel Cancel
YesNo ESC キーボード ショートカットと [閉じる] ボタンは無効になっています。 ユーザーは [はい] または [いいえ] を押す必要があります。
YesNoCancel Cancel

メッセージ ボックスの使用の詳細については、MessageBox および MessageBox サンプルに関するページを参照してください。

関連項目