【发布时间】:2013-02-21 19:20:40
【问题描述】:
我需要从form2->textBox获取数据并放到form1->textBox;
在 Form1.h 我写
#pragma once
#include "Form2.h"
Form2^ f2 = gcnew Form2();
f2->ShowDialog();
这很有效!但在那之后我添加到 Form2.h
#pragma once
#include "Form1.h"
写
Form1^ f1;
f1->textBox1->Text = this.textBox1->Text;
但这给了我很多错误,比如
Form1, Form2, f1, f2 : undeclared identifiers
这是完整的代码
Form1.h
#pragma once
#include "Form2.h"
namespace test76 {
using namespace System;
....
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
....
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form2^ f2 = gcnew Form2();
f2->ShowDialog();
}
};
}
Form2.h
#pragma once
#include "Form1.h" // <- do I need to put it here?
namespace test76 {
using namespace System;
....
public ref class Form2 : public System::Windows::Forms::Form
{
public:
Form2(void)
{
InitializeComponent();
}
protected:
~Form2()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
....
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form1->textBox1->Text = textBox1->Text;
}
};
}
入口点
// test76.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace test76;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
没有#include "Form2.h" 错误是Form1 : undeclared identifier in Form2.h, button1_Click,
#include "Form2.h" 给了我很多错误,例如 Form1、Form2、f2 : undeclared identifiers
【问题讨论】:
-
你有一个循环的#include依赖,form1.h包含form2.h,form2.h包含form1.h。那行不通,C++ 使用单遍编译器。您必须跳 .h/.cpp 舞并使用前向声明。
标签: winforms visual-studio managed-c++