Вопрос

Я пытаюсь использовать этот код, чтобы закрыть форму на конкретном ответе на окно сообщений. Я продолжаю получать ошибку, говоря, что ни один Yes ни No принадлежать 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);
                 }
             }

     }
Это было полезно?

Решение

Есть столкновение между DialogResult перечисление и DialogResult свойство Form. Анкет Вы хотите первое, компилятор предполагает, что вы имеете в виду последнее.

Один из способов разрешения двусмысленности-это полная квалификация вашей ссылки на перечисление:

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

Я нашел второй метод в этом нить; перемещать using namespace System... заявления из namespace Блок, затем обратитесь к Enum через глобальное пространство имен.

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

Другие советы

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

Вот рабочее решение, которое имеет дополнительный код, чтобы вы могли увидеть всю картинку. В этом примере есть некоторые работы 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
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top