我想通过构造函数将Form1传递给Form2,但它不起作用,我想它是从我身边愚蠢但我无法弄清楚出了什么问题 . 我会在这里感谢一些帮助 .
这是Form1包含一个richtextbox和一个按钮,它打开第二个Form .
#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();
}
};
}
这是Form2与有问题的构造函数
#pragma once
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 passForm {
/// <summary>
/// Summary for Form2
///
/// 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 Form2 : public System::Windows::Forms::Form
{
private: System::Windows::Forms::Form ^ 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
}
private: System::Windows::Forms::Button^ btnShowResults;
public:
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form2()
{
if (components)
{
delete components;
}
}
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->btnShowResults = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// btnShowResults
//
this->btnShowResults->Location = System::Drawing::Point(88, 108);
this->btnShowResults->Name = L"btnShowResults";
this->btnShowResults->Size = System::Drawing::Size(99, 23);
this->btnShowResults->TabIndex = 0;
this->btnShowResults->Text = L"Show Results";
this->btnShowResults->UseVisualStyleBackColor = true;
this->btnShowResults->Click += gcnew System::EventHandler(this, &Form2::btnShowResults_Click);
//
// Form2
//
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->btnShowResults);
this->Name = L"Form2";
this->Text = L"Form2";
this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void btnShowResults_Click(System::Object^ sender, System::EventArgs^ e) {
otherform->richTextBox1->AppendText("Hello\n"); //richTextBox1 is not recognized
}
};
}
请注意以下部分
Form1.h
private:System :: Void open_form2_button_Click(System :: Object ^ sender,System :: EventArgs ^ e){
Form^ rgForm = gcnew Form2(this);
rgForm->Show();
//this->Hide();
}
和Form2.h
private:System :: Windows :: Forms :: Form ^ 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
}
我想这些部分会产生错误 .
请注意,当我传递给构造函数时,只有RichTxtBox对象一切正常 .
3 回答
例如 :
Form1.h:
Form1.cpp:
然后在form2.h中
事件,在form2.cpp中
当你执行这段代码时:
让我试着猜一下:
我确定作业有效,但是你这样做:
这不会起作用,因为
System::Windows::Forms::Form
没有成员richTextBox1
.您可能想要定义
Form1
的句柄,而不是Form
.另一方面,我建议您使用事件机制在表单之间进行通信 . 第二种形式触发传递正确参数的事件,第一种形式安装该事件的句柄并进行适当的处理 .
首先,在Form1类中,添加一个将设置RichTextBox值的方法:
然后在你的form2中点击按钮:
它还没有做任何事情来编译,是在Form1 Form2.h文件中做一个早期的声明 . 例如,在第一个命名空间中
它将在Form1.h文件中执行相同的操作:以这种方式声明form2:
你的程序对我很好,如果你有任何问题,请不要犹豫