问题描述
|
我正在尝试使用此代码关闭消息框特定答案上的表单。我不断收到一条错误消息,指出
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
枚举与Form
的DialogResult
属性之间存在命名冲突。您需要前者,编译器假定您是在指后者。
解决歧义的一种方法是完全限定您对枚举的引用:
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?\",MessageBoxIcon::Question) == ::DialogResult::No))
,
if(((MessageBox :: Show(\“ ... \”,\“ Watchdog警告\”,MessageBoxButtons ::是,MessageBoxIcon ::问题)==
系统:: Windows :: Forms :: DialogResult :: No))
{
e-> Cancel = true; //不要关闭
}
, 这是有效的解决方案,其中包含一些额外的代码,因此您可以看到整个图片。在此示例中,必须关闭一些工作ѭ11,然后才能将其关闭。
#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\",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