首页 文章

无法通过构造函数将Windows窗体传递给另一个窗体

提问于
浏览
-2

我想通过构造函数将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 回答

  • 0

    例如 :

    Form1.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);
            }
        };
    }
    

    Form1.cpp:

    #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);
    }
    

    然后在form2.h中

    #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);
        };
    }
    

    事件,在form2.cpp中

    #include "stdafx.h"
    #include "Form2.h"
    
    using namespace ArrayShallow;
    
    System::Void Form2::button1_Click(System::Object^  sender, System::EventArgs^  e)
    {
        currForm1->SetValue("Hello\n");
    }
    

    当你执行这段代码时:

    enter image description here

  • 0

    让我试着猜一下:

    我确定作业有效,但是你这样做:

    otherform->richTextBox1->AppendText("Hello\n");
    

    这不会起作用,因为 System::Windows::Forms::Form 没有成员 richTextBox1 .

    您可能想要定义 Form1 的句柄,而不是 Form .

    private: Form1 ^ otherform;
    

    另一方面,我建议您使用事件机制在表单之间进行通信 . 第二种形式触发传递正确参数的事件,第一种形式安装该事件的句柄并进行适当的处理 .

  • 0

    首先,在Form1类中,添加一个将设置RichTextBox值的方法:

    public:
        void SetValue(System::String ^text)
        {
            richTextBox1->AppendText(text);
        }
    

    然后在你的form2中点击按钮:

    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;
    

    你的程序对我很好,如果你有任何问题,请不要犹豫

相关问题