C ++ Windows窗体应用程序在按钮单击时访问另一个窗体

时间:2014-04-27 16:26:27

标签: c++ visual-studio-2010 windows-forms-designer

我在Visual Studio 2010中使用C ++创建了Windows窗体应用程序。我创建了两个窗体(Form1,Form2)。我在Form1上添加了按钮。现在我想点击按钮转到Form2(隐藏Form1)。

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

             }

这样做的正确方法是什么?

我试过这个:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 Form2->ShowDialog();
             }

但是这在构建时给了我这个错误:

error C2065: 'Form2' : undeclared identifier
error C2227: left of '->ShowDialog' must point to class/struct/union/generic type
1>          type is ''unknown-type''

1 个答案:

答案 0 :(得分:0)

我忘了加入" Form1.h":

#include "Form2.h"

然后在Form1.h中:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                 Form2 ^ form2 = gcnew Form2;
                 form2->Show();
             }