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

これが完全なコードです

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です:Form2.hの未宣言の識別子、button1_Click、

#include "Form2.h"を使用すると、Form1、Form2、f2のような多くのエラーが発生します:宣言されていない識別子

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

2 つのフォームを同時に使用する

これを行う方法は他にもあると確信していますが、確実な方法の 1 つは、既存のフォームの新しいインスタンス (または別のフォーム クラスのインスタンス) をインスタンス化し、その内容をクリアして、新しい内容をフォームに追加することです。 (かなり冗長なので、例から生成されたコードを省略していることに注意してください)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 に答える