【问题标题】:data extange between forms in managed c++托管 C++ 中表单之间的数据交换
【发布时间】:2013-02-21 19:20:40
【问题描述】:

我需要从form2->textBox获取数据并放到form1->textBox;

在 Form1.h 我写

#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

这是完整的代码

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

             }
    };
}

Form2.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

【问题讨论】:

  • 你有一个循环的#include依赖,form1.h包含form2.h,form2.h包含form1.h。那行不通,C++ 使用单遍编译器。您必须跳 .h/.cpp 舞并使用前向声明。

标签: winforms visual-studio managed-c++


【解决方案1】:

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

具体到您的错误问题

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

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 是一种无需指定已知类型的方法。

【讨论】:

  • 这不是我所需要的。你已经将数据传递给 Form2,但我需要的是在那之后将数据从 Form2 传递给 Form1。我在标题帖子中给了你完整的代码。
【解决方案2】:

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

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

您不能直接使用Form1-&gt;textBox1。您需要一个可以捕获地址/指向运行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; }

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 2013-04-04
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多