共用方式為


如何開啟訊息方塊 (WPF .NET)

訊息方塊 是一個對話方塊,可用來快速顯示資訊,並選擇性地允許使用者做出決策。 類別會提供訊息方塊的 MessageBox 存取權。 訊息方塊會以強制回應方式 顯示 。 顯示訊息方塊的程式碼會暫停,直到使用者使用關閉按鈕或回應按鈕關閉訊息方塊為止。

下圖示范訊息方塊的部分:

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

  • 標題列,具有標題 (1)。
  • 關閉按鈕 (2) 。
  • 圖示 (3)。
  • 向使用者顯示的訊息 (4)。
  • 回應按鈕 (5)。

若要呈現或收集複雜的資料,對話方塊可能比訊息方塊更合適。 如需詳細資訊,請參閱 對話方塊概觀

顯示訊息方塊

若要建立訊息方塊,您可以使用 類別 MessageBoxMessageBox.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 範例

另請參閱