WPF MessageBox出现两次

时间:2012-07-05 09:03:22

标签: c# wpf

我正在使用以下代码显示带有ok和cancel按钮的MessageBox。当我单击任何按钮时,会再次出现相同的消息框。这段代码有什么问题吗?  

        string messageBoxText = "Uploading Data";
        string caption = "Upload Data";
        MessageBoxButton button = MessageBoxButton.OKCancel;                
        // Display message box
        MessageBox.Show(messageBoxText, caption, button, icon);
        MessageBoxResult res = MessageBox.Show(messageBoxText, caption, button, icon);
        if (res == MessageBoxResult.OK)
        {
           count++;              
        }

string messageBoxText = "Uploading Data"; string caption = "Upload Data"; MessageBoxButton button = MessageBoxButton.OKCancel; // Display message box MessageBox.Show(messageBoxText, caption, button, icon); MessageBoxResult res = MessageBox.Show(messageBoxText, caption, button, icon); if (res == MessageBoxResult.OK) { count++; }

2 个答案:

答案 0 :(得分:5)

因为您要拨打MessageBox.Show两次......

string messageBoxText = "Uploading Data";
    string caption = "Upload Data";
    MessageBoxButton button = MessageBoxButton.OKCancel;                
    // Display message box
    MessageBox.Show(messageBoxText, caption, button, icon); //**Comment this line**
    MessageBoxResult res = MessageBox.Show(messageBoxText, caption, button, icon);
    if (res == MessageBoxResult.OK)
    {
       count++;              
    }

答案 1 :(得分:2)

您通过MessageBox.Show呼叫消息框两次。您可能想要删除第一个

MessageBox.Show(messageBoxText, caption, button, icon);