Aracılığıyla paylaş


İleti kutusunu açma (WPF .NET)

İleti kutusu, bilgileri hızla görüntülemek ve isteğe bağlı olarak kullanıcıların karar vermesine izin vermek için kullanılan bir iletişim kutusudur. İleti kutusuna erişim sınıfı tarafından MessageBox sağlanır. Bir ileti kutusu modlu olarak görüntülenir. Ayrıca, kullanıcı kapat düğmesiyle veya yanıt düğmesiyle ileti kutusunu kapatana kadar ileti kutusunu görüntüleyen kod duraklatılır.

Aşağıdaki çizimde ileti kutusunun bölümleri gösterilmektedir:

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

  • başlık (1) içeren başlık çubuğu.
  • Kapat düğmesi (2).
  • Simge (3).
  • Kullanıcıya görüntülenen ileti (4).
  • Yanıt düğmeleri (5).

Karmaşık verileri sunmak veya toplamak için, bir iletişim kutusu ileti kutusundan daha uygun olabilir. Daha fazla bilgi için bkz . İletişim kutularına genel bakış.

İleti kutusu görüntüleme

İleti kutusu oluşturmak için sınıfını MessageBox kullanırsınız. yöntemi, MessageBox.Show aşağıdaki kodda gösterilen ileti kutusu metnini, başlığını, simgesini ve düğmelerini yapılandırmanızı sağlar:

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 Yöntem aşırı yüklemeleri, ileti kutusunu yapılandırmanın yollarını sağlar. Bu seçenekler şunlardır:

  • Başlık çubuğu başlık
  • İleti simgesi
  • İleti metni
  • Yanıt düğmeleri

burada ileti kutusu kullanmanın bazı diğer örnekleri verilmiştir.

  • Uyarı görüntüleme.

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

    Önceki kod aşağıdaki görüntüye benzer bir ileti kutusu görüntüler:

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

    İleti kutusu sınıfı tarafından sağlanan seçenekleri kullanmak iyi bir fikirdir. Önceki uyarıyla aynı uyarıyı kullanarak görsel açıdan daha çekici hale getirmek için daha fazla seçenek belirleyin:

    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)
    

    Önceki kod aşağıdaki görüntüye benzer bir ileti kutusu görüntüler:

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

  • Bir uyarı görüntüleyin.

    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)
    

    Önceki kod aşağıdaki görüntüye benzer bir ileti kutusu görüntüler:

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

  • Kullanıcıya bir soru sorun.

    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
    

    Önceki kod aşağıdaki görüntüye benzer bir ileti kutusu görüntüler:

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

İleti kutusu yanıtını işleme

yöntemi ileti MessageBox.Show kutusunu görüntüler ve bir sonuç döndürür. Sonuç, kullanıcının ileti kutusunu nasıl kapatmış olduğunu gösterir:

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

Kullanıcı ileti kutusunun en altındaki düğmelere bastığında, karşılık gelen MessageBoxResult döndürülür. Ancak, kullanıcı ESC tuşuna basarsa veya Kapat düğmesine basarsa (ileti kutusu çiziminde#2), ileti kutusunun sonucu düğme seçeneklerine göre değişir:

Düğme seçenekleri ESC veya Kapat düğmesi sonucu
OK OK
OKCancel Cancel
YesNo ESC klavye kısayolu ve Kapat düğmesi devre dışı. Kullanıcı Evet veya Hayır'a basmalıdır.
YesNoCancel Cancel

İleti kutularını kullanma hakkında daha fazla bilgi için bkz MessageBox . ve MessageBox örneği.

Ayrıca bkz.