【问题标题】:Close form on messagebox answer trouble关闭消息框回答问题的表格
【发布时间】:2011-09-14 16:19:26
【问题描述】:

我正在尝试使用此代码关闭消息框特定答案的表单。我不断收到一条错误消息,指出 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);
                 }
             }

     }

【问题讨论】:

  • 这是一个 C++ 问题,它没有为类型标识符保留单独的符号表。您必须完整输入名称以避免与 Form::DialogResult 产生歧义。

标签: visual-studio-2010 visual-c++ messagebox formclosing dialogresult


【解决方案1】:
if((MessageBox::Show("...", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::No)) { e->取消=真; //不要关闭 }

【讨论】:

    【解决方案2】:

    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 块,然后通过全局命名空间引用枚举。

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

    【讨论】:

      【解决方案3】:

      这是一个有效的解决方案,其中包含一些额外的代码,因此您可以看到整个画面。在这个例子中,有一些工作 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-07
        • 1970-01-01
        • 2011-07-28
        • 1970-01-01
        • 2021-02-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多