Winforms 无法通过构造函数将Windows窗体传递给其他窗体
我想通过构造函数将Form1传递给Form2,但它不起作用,我想这是我这边的一件愚蠢的事情,但我不知道出了什么问题。我希望能在这里得到一些帮助 下面是Form1,它包含一个richtextbox和一个按钮,用于打开第二个表单Winforms 无法通过构造函数将Windows窗体传递给其他窗体,winforms,visual-studio,constructor,c++-cli,Winforms,Visual Studio,Constructor,C++ Cli,我想通过构造函数将Form1传递给Form2,但它不起作用,我想这是我这边的一件愚蠢的事情,但我不知道出了什么问题。我希望能在这里得到一些帮助 下面是Form1,它包含一个richtextbox和一个按钮,用于打开第二个表单 #pragma once #include "Form2.h" namespace passForm { using namespace System; using namespace System::ComponentModel; usin
#pragma once
#include "Form2.h"
namespace passForm {
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
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public: System::Windows::Forms::RichTextBox^ richTextBox1;
public: System::Windows::Forms::Button^ open_form2_button;
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
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->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->open_form2_button = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// richTextBox1
//
this->richTextBox1->Location = System::Drawing::Point(12, 12);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(100, 96);
this->richTextBox1->TabIndex = 0;
this->richTextBox1->Text = L"";
//
// open_form2_button
//
this->open_form2_button->Location = System::Drawing::Point(159, 47);
this->open_form2_button->Name = L"open_form2_button";
this->open_form2_button->Size = System::Drawing::Size(89, 23);
this->open_form2_button->TabIndex = 1;
this->open_form2_button->Text = L"Open Form 2";
this->open_form2_button->UseVisualStyleBackColor = true;
this->open_form2_button->Click += gcnew System::EventHandler(this, &Form1::open_form2_button_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->open_form2_button);
this->Controls->Add(this->richTextBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void open_form2_button_Click(System::Object^ sender, System::EventArgs^ e) {
Form^ rgForm = gcnew Form2(this);
rgForm->Show();
//this->Hide();
}
};
}
表格2.h
私有:系统::Windows::窗体::窗体^otherform
public:
Form2()
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
public:
Form2(System::Windows::Forms::Form ^ frm1)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
otherform = frm1; // this assignment is not working
}
我猜这些部分会产生错误
请注意,当我只向构造函数传递richttxtbox对象时,一切都正常工作。让我试着猜测一下:
我肯定作业是有效的,但你要这样做:
otherform->richTextBox1->AppendText("Hello\n");
这是行不通的,因为System::Windows::Forms::Form没有成员richTextBox1
您可能想要定义Form1的句柄,而不是Form
另一方面,我建议您使用事件机制在表单之间进行通信。第二个表单触发一个传递正确参数的事件,第一个表单安装该事件的句柄并进行适当的处理。首先,在Form1类中,添加一个将设置RichTextBox值的方法:
public:
void SetValue(System::String ^text)
{
richTextBox1->AppendText(text);
}
然后在表单2中,单击按钮:
System::Void Form2::button1_Click(System::Object^ sender, System::EventArgs^ e)
{
currForm1->SetValue("Hello\n");
}
它还没有做任何事情来编译它,就是在Form1 Form2.h文件中做一个早期语句。例如,在第一个命名空间中
ref class Form1;
它将在Form1.h文件中执行相同的操作:通过以下方式声明form2:
ref class Form2;
您的程序对我很好,如果您有任何问题,请不要犹豫,例如:
表格1.h:
#pragma once
#include "Form2.h"
namespace ArrayShallow {
ref class Form2;
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>
/// Description résumée de Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ajoutez ici le code du constructeur
//
}
protected:
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
void InitializeComponent(void)
{
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
this->richTextBox1->Location = System::Drawing::Point(32, 25);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(100, 96);
this->richTextBox1->TabIndex = 0;
this->richTextBox1->Text = L"";
this->button1->Location = System::Drawing::Point(153, 157);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Controls->Add(this->richTextBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private:
Form2 ^ currForm2;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e);
private: System::Void button1_Click(System::Object ^sender, System::EventArgs^ e);
public:
void SetValue(System::String ^text)
{
richTextBox1->AppendText(text);
}
};
}
执行此代码时:
你的代码是C++ + CLI而不是C++。请适当标记您的帖子。谢谢您的通知,先生。不,它不起作用。对于我的实现,我采取以下措施:错误C2039:“richTextBox1”:不是“System::Windows::Forms::Form”的成员d:\visualstudio2008projects\training\passform\passform\Form2.h 114根据您的建议,我采取以下措施:错误C2039:“Form1”:不是'System::Windows::Forms'd:\visualstudio2008projects\training\passform\passform\Form2.h 25抱歉,我刚刚在代码中替换了Form1。我已经更新了我的答案。在表格1.h中,我更改了行形式^rgForm=gcnew Form2this;使用Form2^rgForm=gcnew Form2此;在Form2.h中,我更改了行private:System::Windows::Forms::Form^otherform;使用private:Form1^otherform。现在,在其他编译错误中,我收到了错误C2065:“otherform”:未声明的标识符。感谢您的回复。你能更具体地谈谈那些早期的声明吗?你能举个例子吗?只需去掉Form1.cpp中包含主函数的部分。我必须说,在我所看过的书中,有很多干净的C++/CLI代码。结论是,我必须对名称空间和include进行更多的研究。非常感谢你!
ref class Form1;
ref class Form2;
#pragma once
#include "Form2.h"
namespace ArrayShallow {
ref class Form2;
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>
/// Description résumée de Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ajoutez ici le code du constructeur
//
}
protected:
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::RichTextBox^ richTextBox1;
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
void InitializeComponent(void)
{
this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
this->richTextBox1->Location = System::Drawing::Point(32, 25);
this->richTextBox1->Name = L"richTextBox1";
this->richTextBox1->Size = System::Drawing::Size(100, 96);
this->richTextBox1->TabIndex = 0;
this->richTextBox1->Text = L"";
this->button1->Location = System::Drawing::Point(153, 157);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Controls->Add(this->richTextBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private:
Form2 ^ currForm2;
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e);
private: System::Void button1_Click(System::Object ^sender, System::EventArgs^ e);
public:
void SetValue(System::String ^text)
{
richTextBox1->AppendText(text);
}
};
}
#include "stdafx.h"
#include "Form1.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace ArrayShallow;
System::Void Form1::Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
currForm2 = gcnew Form2(this);
}
System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e)
{
currForm2->Show();
}
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return (0);
}
#pragma once
#include "Form1.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
namespace ArrayShallow {
ref class Form1;
/// <summary>
/// Description résumée de Form2
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
private:
Form1 ^currForm1;
public:
Form2(Form1 ^theForm1)
{
InitializeComponent();
//
//TODO: ajoutez ici le code du constructeur
//
currForm1 = theForm1;
}
protected:
/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
~Form2()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(135, 107);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e);
};
}
#include "stdafx.h"
#include "Form2.h"
using namespace ArrayShallow;
System::Void Form2::button1_Click(System::Object^ sender, System::EventArgs^ e)
{
currForm1->SetValue("Hello\n");
}