1

我正在試圖使用此代碼來關閉消息框的特定答案上的窗體。我一直收到一個錯誤,說YesNo都不屬於DialogResult::。我基本上直接從MS站點複製這段代碼,所以我不知道什麼是錯的。幫幫我?消息框上的關閉窗體回答問題

private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) { 
      if(!watchdog->Checked) 
      { 
       if((MessageBox::Show("CAN Watchdog is currently OFF. If you exit with these settings, the SENSOWheel will still be engaged. To prevent this, please enable CAN Watchdog before closing. Would you still like to quit?", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == DialogResult::No)) 
       { 
        return; 
       } 
       else 
       { 
        close_Click(this, e); 
       } 
      } 

    } 
+0

這是一個C++問題,它沒有爲類型標識符保留單獨的符號表。您必須完整輸入名稱以避免Form :: DialogResult帶來的歧義。 – 2011-06-14 13:21:37

回答

0

還有就是DialogResult枚舉之間的衝突的命名,以及FormDialogResult財產。你想要前者,編譯器假定你指的是後者。解決歧義

一種方式是完全符合您的參考枚舉:

if((MessageBox::Show("CAN Watchdog ... Would you still like to quit?", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::No)) 

我在這個thread找到了第二個方法;將using namespace System...語句移出namespace塊,然後通過全局名稱空間引用enum。

if((MessageBox::Show("CAN Watchdog ... Would you still like to quit?", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == ::DialogResult::No)) 
+0

謝謝!這工作完美! – jlehenbauer 2011-06-17 18:37:39

1
 
if((MessageBox::Show("...", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == 
    System::Windows::Forms::DialogResult::No))     
{ 
    e->Cancel = true; // don't close    
}     
+0

如果沒有編譯,請發佈確切的錯誤消息。 – 2011-06-14 13:08:25

0

這是工作解決方案,它有一些額外的代碼,所以你可以看到整個圖片。在這個例子中,有一些工作BackgroundWorker必須在應用程序關閉之前停止。

#pragma region Start/Stop/Exit 

    private: System::Void backgroundWorker1_RunWorkerCompleted(System::Object^ sender, System::ComponentModel::RunWorkerCompletedEventArgs^ e) { 
       if(e->Cancelled)  
       { 
        rtbLog->Text = rtbLog->Text + ">>> Application stopped \n"; 
       } 
       else 
       { 
        rtbLog->Text = rtbLog->Text + ">>> Application completed \n"; 
       } 
      } 

    private: System::Void startToolStripMenuItemStart_Click(System::Object^ sender, System::EventArgs^ e) 
      { 
       if (backgroundWorker1->IsBusy == false) 
       { 
        backgroundWorker1->RunWorkerAsync(1); //starting background worker 
       } 
      } 

    private: System::Void stopToolStripMenuItemStop_Click(System::Object^ sender, System::EventArgs^ e) 
      { 
       if (backgroundWorker1->IsBusy == true && backgroundWorker1->WorkerSupportsCancellation == true) 
       {  
        backgroundWorker1->CancelAsync(); 
       } 
      }  

    private: System::Void Form1_FormClosing(System::Object^ sender, System::Windows::Forms::FormClosingEventArgs^ e) { 

       if((MessageBox::Show("Would you still like to quit?", "Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == 
        System::Windows::Forms::DialogResult::No))     
       { 
        e->Cancel = true; // Don't close and BackgroundWoker is executing.    
       } 
       else 
       { 
        if (backgroundWorker1->IsBusy == true && backgroundWorker1->WorkerSupportsCancellation == true) 
        {  
         backgroundWorker1->CancelAsync(); 
        } 
       } 
      } 

    private: System::Void exitToolStripMenuItemExit_Click(System::Object^ sender, System::EventArgs^ e) { 

       Application::Exit(); // The user wants to exit the application. Close everything down. 

      } 

#pragma endregion