关闭消息框上的表单回答问题

发布于 2024-11-15 03:28:49 字数 870 浏览 5 评论 0原文

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

     }

I am trying to use this code to close a form on a specific answer of a message box. I keep receiving an error saying that neither Yes nor No belong to DialogResult::. I basically copied this code straight from the MS site, so i have no idea what's wrong. Help?

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);
                 }
             }

     }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与往事干杯 2024-11-22 03:28:49
if((MessageBox::Show("...", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == 
    System::Windows::Forms::DialogResult::No))                  
{
   e->Cancel = true;    // don't close              
}                  
if((MessageBox::Show("...", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == 
    System::Windows::Forms::DialogResult::No))                  
{
   e->Cancel = true;    // don't close              
}                  
妄司 2024-11-22 03:28:49

DialogResult 枚举与 FormDialogResult 属性之间存在命名冲突。您想要前者,编译器假设您指的是后者。

解决歧义的一种方法是完全限定对枚举的引用:

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

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

There is a naming clash between the DialogResult enumeration, and the DialogResult property of Form. You want the former, the compiler assumes you are referring to the latter.

One way to resolve the ambiguity is to fully-qualify your reference to the enum:

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

I found a second method in this thread; move the using namespace System... statements out of the namespace block, then refer to the enum via the global namespace.

if((MessageBox::Show("CAN Watchdog ... Would you still like to quit?", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == ::DialogResult::No))
好菇凉咱不稀罕他 2024-11-22 03:28:49

这是工作解决方案,其中有一些额外的代码,以便您可以看到整个图片。在此示例中,有一些正在运行的 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

Here is working solution which has some extra code so you can see the whole picture. In this example there is some working BackgroundWorker that must be stopped before app is gonna be closed.

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文