2

我需要从 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

4

2 回答 2

0

看起来你还没有初始化你textBox1我说的样子,因为我没有看到你背后的设计器代码Form1.h(但这只是一个即兴猜测,Windows Form Designer generated code在你的标题中寻找类似的东西)。

具体到您的错误问题

如果您想在运行时生成文本框,那么这样的东西将允许访问文本框

int main (array <System :: String ^> ^ args)
{
   // Run the application 

   auto form = gcnew MyForm();
   auto myTextBox = gcnew Windows::Forms::TextBox();
   myTextBox->Name = "myTxtBox";
   myTextBox->Text = "This is my txt box";
   form->Controls->Add(myTextBox);
   Application::Run(form);

   // return to main
   return 0;
}

但是,如果您正在使用设计器并已命名控件,textBox1那么我建议您检查设计器的属性并确保该Modifiers属性设置为其他内容Private并且您的Name属性匹配。如果这两个都是真的,那么以下应该工作。

int main (array <System :: String ^> ^ args)
{
   // Run the application 

   auto form = gcnew MyForm();
   form->myDesignTimeTextbox->Text = "I am a design time textbox";
   Application::Run(form);

   // return to main
   return 0;
}

同时使用两个表格

虽然我确信还有其他方法可以做到这一点,但一种可靠的方法是实例化现有表单的新实例(或另一个表单类的实例),清除其内容并将新内容附加到表单。 (注意我省略了示例中生成的代码,因为它非常冗长)查看button1_Click event

例子

表单类


public ref class MyForm : public System::Windows::Forms::Form
   {
   public:
      MyForm(void)
      {
         InitializeComponent();
         //
         //TODO: Add the constructor code here
         //
      }

   protected:
      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      ~MyForm()
      {
         if (components)
         {
            delete components;
         }
      }
   public: System::Windows::Forms::TextBox^  myDesignTimeTextbox;
   protected: System::Windows::Forms::Button^  button1;
   public: 

   public: 
   protected: 

   protected: 

   protected: 

   private:
      /// <summary>
      /// Required designer variable.
      /// </summary>
      System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
 //omitted for brevity
//I have a button named button1 and a TextBox named myDesignTimeTextbox
#pragma endregion

   private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
            {
               this->myDesignTimeTextbox->Text = L"You clicked Teh Button, You R Win";

               //create new form and add a textbox
               auto form2 = gcnew MyForm();
               form2->Controls->Clear();
               auto getTxt = gcnew TextBox();
               getTxt->Name = L"Generated_Textbox";

               //the content of the txtbox will be that of the design Time text box
               getTxt->Text = myDesignTimeTextbox->Text;
               form2->Controls->Add(getTxt);

               form2->ShowDialog();
            }

   };

入口点


 int main (array  ^ args)
   {
      // Run the application 
      auto form1 = gcnew MyForm();

      Application::Run(form1);

      // return to main
      return 0;
   }

注意* 如果您还不知道,关键字auto是一种无需指定已知类型的方法。

于 2013-02-22T14:50:36.743 回答
0

唯一的问题在于您的Form2.h代码:

//Form2.h
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                Form1->textBox1->Text = textBox1->Text;
         }

不能Form1->textBox1直接使用。您需要一个可以捕获地址/指向 running 的变量form1。将其修改为如下内容:

//Form1.h
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
            Form2^ f2 = gcnew Form2(this);
            f2->ShowDialog();
         }

//Form2
private: Form1^ f1;
Form2(Form1^ f)
{ f1=f; }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
         { f1->textBox1->Text = textBox1->Text; }
于 2015-09-13T11:32:44.833 回答