C++ 什么是DialogResult函数visual C++;2010?

C++ 什么是DialogResult函数visual C++;2010?,c++,visual-studio,visual-studio-2010,visual-c++,C++,Visual Studio,Visual Studio 2010,Visual C++,我正在做一个简单的表格,我花了一天多的时间去发现一些东西 我想要的是点击表单1中的一个按钮,然后弹出一个表单2。我有以下代码 private: System::Void MyAdd_Click(System::Object^ sender, System::EventArgs^ e) { Form2^ myForm2 = gcnew Form2(); } }; 但是,form2无法弹出。我真的不明白,所以我从一个例子中复制了更多的代码。尽管我不认为它会有用,但

我正在做一个简单的表格,我花了一天多的时间去发现一些东西

我想要的是点击表单1中的一个按钮,然后弹出一个表单2。我有以下代码

private: System::Void MyAdd_Click(System::Object^  sender, System::EventArgs^  e) {
         Form2^ myForm2 = gcnew Form2();

     }
};
但是,form2无法弹出。我真的不明白,所以我从一个例子中复制了更多的代码。尽管我不认为它会有用,但还是试试看。不管它如何工作

private: System::Void MyAdd_Click(System::Object^  sender, System::EventArgs^  e) {
         Form2^ myForm2 = gcnew Form2();
         if (myForm2->ShowDialog()==System::Windows::Forms::DialogResult::OK) {}
     }
};

我的问题是,我已经在这两种情况下创建了表单,为什么IF语句会有所不同呢?

在这种情况下,它没有任何区别,因为没有做任何额外的操作。但是,一种常见用法是:

选中可以查看对话框是如何关闭的

如果您询问为什么需要调用
ShowDialog
,这是因为即使您已经创建了表单,您也没有告诉系统显示表单。请注意,
ShowDialog
blocks/在对话框关闭之前不会返回

void ShowMyDialogBox()
{
   Form2^ testDialog = gcnew Form2;

   // Show testDialog as a modal dialog and determine if DialogResult = OK.
   if ( testDialog->ShowDialog( this ) == ::DialogResult::OK )
   {

      // Read the contents of testDialog's TextBox.
      this->txtResult->Text = testDialog->TextBox1->Text;
   }
   else
   {
      this->txtResult->Text = "Cancelled";
   }

   delete testDialog;
}