Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C+中打开新表单+;Windows窗体应用程序 < >我使用VisualStudio 2012在C++中使用Windows窗体。我从这个链接得到了帮助_C++_Forms_Winforms - Fatal编程技术网

在C+中打开新表单+;Windows窗体应用程序 < >我使用VisualStudio 2012在C++中使用Windows窗体。我从这个链接得到了帮助

在C+中打开新表单+;Windows窗体应用程序 < >我使用VisualStudio 2012在C++中使用Windows窗体。我从这个链接得到了帮助,c++,forms,winforms,C++,Forms,Winforms,我想要多张表格。我设计了Form2,并将Form2.h包含在Form1.h中。但当我打开form2时,它立即出现并消失。 这是我的代码: #include "Form2.h" ... private: System::void button_Click(System::Object^ sender, System::EventArgs^ e){ Form2 frm2; frm2.Show(); //this->Hide(); //this->Clos

我想要多张表格。我设计了Form2,并将Form2.h包含在Form1.h中。但当我打开form2时,它立即出现并消失。 这是我的代码:

#include "Form2.h"
...

private: System::void button_Click(System::Object^ sender, System::EventArgs^ e){
    Form2 frm2;
    frm2.Show();
    //this->Hide();
    //this->Close();
}
如果我使用

this->Hide();
这两个窗体将隐藏,如果我关闭form1

this->Close();
form2也将关闭

我想独立地打开和关闭表单。我该怎么办


任何帮助都将不胜感激

令人惊讶的是,在VS2012中删除项目模板会立即导致每个人都编写错误的代码。您使用的是“堆栈语义”,它是C++中RAII模式的仿真。或者换句话说,当按钮返回时,Form2实例会立即被销毁。正确的代码如下所示:

   Form2^ frm2 = gcnew Form2;
   frm2->Show();
创建Form1实例的代码中存在完全相同的错误,您必须通过
%Form1
才能看到该错误。这不太明显,因为Main()方法在应用程序的生命周期内一直保持执行。然而,Form1类的析构函数将被调用两次。当您更改默认设置时,可能会造成严重破坏。相同的配方,不使用堆栈语义:

   Form1^ mainWindow = gcnew Form1;
   Application::Run(mainWindow);
或者简单地说:

   Application::Run(gcnew Form1);

当您调用
this->Close()
时,您的应用程序会立即终止,因为您正在关闭应用程序的主窗口。这是因为您将Form1实例传递给了Application::Run()。这与绝大多数Windows应用程序的行为方式兼容,关闭“主窗口”将结束应用程序

但这不是您想要的,所以不要将表单实例传递给Run()。你需要为你的应用程序设置另一个退出条件,通常你需要一个“当没有更多窗口时”的条件。将Main()方法更改为如下所示:

void OnFormClosed(System::Object ^sender, System::Windows::Forms::FormClosedEventArgs ^e) {
    Form^ form = safe_cast<Form^>(sender);
    form->FormClosed -= gcnew FormClosedEventHandler(&OnFormClosed);
    if (Application::OpenForms->Count == 0) Application::Exit();
    else Application::OpenForms[0]->FormClosed += gcnew FormClosedEventHandler(&OnFormClosed);
}

[STAThread]
int main(array<System::String ^> ^args) {
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    Form1^ startupWindow = gcnew Form1;
    startupWindow->FormClosed += gcnew FormClosedEventHandler(&OnFormClosed);
    startupWindow->Show();
    Application::Run();
    return 0;
}
void OnFormClosed(系统::对象^sender,系统::窗口::表单::FormClosedEventArgs ^e){
表格^Form=安全铸件(发送方);
表单->表单关闭-=gcnewformclosedeventhandler(&OnFormClosed);
如果(Application::OpenForms->Count==0)Application::Exit();
else应用程序::OpenForms[0]->FormClosed+=gcnew-FormClosedEventHandler(&OnFormClosed);
}
[状态线程]
int main(数组^args){
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Form1^ startupWindow=gcnew Form1;
startupWindow->FormClosed+=gcnew FormClosedEventHandler(&OnFormClosed);
启动窗口->显示();
Application::Run();
返回0;
}

<代码>你是否打开了C++的文件?@ Eliyahu Shmuel,我已经使用了上面提到的链接,用C++中的表单工作。打开第一个表单时,我使用
Application::Run(%form)