Search code examples
visual-studio-2010visual-c++messageboxformclosingdialogresult

Close form on messagebox answer trouble


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

     }

Solution

  • 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))