Close form on messagebox answer trouble
I am trying to use this code to close a form on a specific answer of a messag开发者_JAVA百科e 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);
}
}
}
if((MessageBox::Show("...", "Watchdog Warning", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::No)) { e->Cancel = true; // don't close }
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))
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
精彩评论