我需要从 form2->textBox 获取数据并放入 form1->textBox;
在 Form1.hi 中写
#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
这是完整的代码
表格1.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();
}
};
}
表格2.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