所以,我有一个简单的表单应用程序:我有一个表单 1 ( Form1.h ) 和第二个表单 ( Form2.h )。Form1 由一个简单的Add User 按钮组成(在这个阶段,它什么都不做,只是打开 form2 并隐藏 form1)。(到此为止,一切正常)。
但是,当我向应该再次打开 form1的 Form2 添加一个“完成”按钮时,出现以下错误: 左侧 -> 显示必须指向类/结构/联合/通用类型 f1:未声明的标识符 Form1:未声明的标识符 请帮助!代码是:
//Project Name: MultipleForms //Form1.h #ifndef FORM1_H #define FORM1_H #pragma once #include "Form2.h" namespace MultipleForms { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ btnAddUser; private: System::Windows::Forms::Button^ btnExit; protected: private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->btnAddUser = (gcnew System::Windows::Forms::Button()); this->btnExit = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // btnAddUser // this->btnAddUser->Location = System::Drawing::Point(78, 44); this->btnAddUser->Name = L"btnAddUser"; this->btnAddUser->Size = System::Drawing::Size(75, 23); this->btnAddUser->TabIndex = 0; this->btnAddUser->Text = L"Add User"; this->btnAddUser->UseVisualStyleBackColor = true; this->btnAddUser->Click += gcnew System::EventHandler(this, &Form1::btnAddUser_Click); // // btnExit // this->btnExit->Location = System::Drawing::Point(263, 136); this->btnExit->Name = L"btnExit"; this->btnExit->Size = System::Drawing::Size(75, 23); this->btnExit->TabIndex = 1; this->btnExit->Text = L"Exit"; this->btnExit->UseVisualStyleBackColor = true; this->btnExit->Click += gcnew System::EventHandler(this, &Form1::btnExit_Click); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(410, 200); this->Controls->Add(this->btnExit); this->Controls->Add(this->btnAddUser); this->Name = L"Form1"; this->Text = L"Form1"; this->ResumeLayout(false); } #pragma endregion private: System::Void btnAddUser_Click(System::Object^ sender, System::EventArgs^ e) { Form2^ f2 = gcnew Form2(); f2->Show(); this->Hide(); } private: System::Void btnExit_Click(System::Object^ sender, System::EventArgs^ e) { exit(1); } }; } #endif //Form2.h #ifndef FORM2_H #define FORM2_H #pragma once #include "Form1.h" #include<stdlib.h> namespace MultipleForms { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form2 /// </summary> public ref class Form2 : public System::Windows::Forms::Form { public: Form2(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form2() { if (components) { delete components; } } private: System::Windows::Forms::Button^ btnDone; protected: protected: private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> void InitializeComponent(void) { this->btnDone = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // btnDone // this->btnDone->Location = System::Drawing::Point(98, 110); this->btnDone->Name = L"btnDone"; this->btnDone->Size = System::Drawing::Size(75, 23); this->btnDone->TabIndex = 0; this->btnDone->Text = L"Done"; this->btnDone->UseVisualStyleBackColor = true; this->btnDone->Click += gcnew System::EventHandler(this, &Form2::btnDone_Click); // // Form2 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(292, 273); this->Controls->Add(this->btnDone); this->Name = L"Form2"; this->Text = L"Form2"; this->ResumeLayout(false); } #pragma endregion private: System::Void btnDone_Click(System::Object^ sender, System::EventArgs^ e) { Form1^ f1 = gcnew Form1(); f1->Show(); this->Hide(); } }; } #endif
问问题
5050 次
2 回答
1
问题是因为您将 Form1.h 相互包含到 Form2.h 中,反之亦然。因此,在 btnAddUser_Click 和 btnDone_Click 中的引用中需要它们之前,它们的类定义并不完全已知。解决方案是定义其中一项或两项(即,在独立的.cpp 文件中)。
于 2013-04-27T19:50:41.197 回答
0
您不需要在表格2中添加“显示”代码。您只需在表格1中输入即可。因为当您单击 form1 中的该按钮时,会显示 form2 而隐藏 form1。下一行说它应该再次打开。所以当你关闭form2时,form1可以再次打开。
但我认为你和我有同样的问题,就是以两种形式进行交流,反之亦然。我想知道如何将一条消息发送到 form2,然后将一条消息从 form2 发送到 form1。我认为你有同样的问题。
于 2018-07-30T01:54:28.147 回答