0

メニューのオプションを押した後(つまり、子フォームを開くため)、Form1(親)のtextBox1の値をForm2(子)のtextBox2に表示する必要があります。基本的に親子のコミュニケーションの問題。私はこれを変更して必要なことを実行しようとしましが、運が悪かったのです。

Used_values.h(30) : warning C4101: 'HERE' : unreferenced local variable
Used_values.h(560) : error C2065: 'HERE' : undeclared identifier

ここに私のコードの関連部分を見つけることができます:

Form1(親):

#pragma once
#include "Used_values.h"

namespace Vp_vs_gasconcentration_WindowsForm {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();
        }

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::MenuStrip^  menuStrip1_generalMenu;
    private: System::Windows::Forms::ToolStripMenuItem^  usedValues_ToolStripMenuItem;
    private: System::Windows::Forms::TextBox^  textBox1_waterDepth;
    //(more code...)

    private:
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            //(more code...)
            this->textBox1_waterDepth = (gcnew System::Windows::Forms::TextBox());
            //(more code...)
            // 
            // textBox1_waterDepth
            // 
            this->textBox1_waterDepth->Font = (gcnew System::Drawing::Font(L"Segoe UI", 10));
            this->textBox1_waterDepth->Location = System::Drawing::Point(336, 185);
            this->textBox1_waterDepth->Name = L"textBox1_waterDepth";
            this->textBox1_waterDepth->Size = System::Drawing::Size(100, 25);
            this->textBox1_waterDepth->TabIndex = 1;
            this->textBox1_waterDepth->Text = L"2170";
            this->textBox1_waterDepth->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
            this->textBox1_waterDepth->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_waterDepth_TextChanged);
            //(more code...)
        }

#pragma endregion
    //(more code...)
    private: System::Void usedValues_ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
                 Used_values^ used_values = gcnew Used_values();
                 used_values->StartPosition = FormStartPosition::CenterParent;
                 used_values->ShowDialog();
                 used_values->HERE = textBox1_waterDepth->Text;
             }
    //(more code...)
};
}

Used_values(子):

#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 Vp_vs_gasconcentration_WindowsForm {

    public ref class Used_values : public System::Windows::Forms::Form
    {
    public:
        Used_values(void)
        {
            InitializeComponent();
            String ^HERE;
        }

    protected:
        ~Used_values()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::TextBox^  textBox1_waterDepthUsedValues;
    //(more code...)

    private:
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            //(more code...)
            this->textBox1_waterDepthUsedValues = (gcnew System::Windows::Forms::TextBox());
(more code...)
            // 
            // textBox1_waterDepthUsedValues
            // 
            this->textBox1_waterDepthUsedValues->Location = System::Drawing::Point(435, 10);
            this->textBox1_waterDepthUsedValues->Name = L"textBox1_waterDepthUsedValues";
            this->textBox1_waterDepthUsedValues->Size = System::Drawing::Size(100, 25);
            this->textBox1_waterDepthUsedValues->TabIndex = 1;
            this->textBox1_waterDepthUsedValues->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
            this->textBox1_waterDepthUsedValues->TextChanged += gcnew System::EventHandler(this, &Used_values::textBox1_waterDepthUsedValues_TextChanged);
            //(more code...)
        }
#pragma endregion

private: System::Void textBox1_waterDepthUsedValues_TextChanged(System::Object^  sender, System::EventArgs^  e) {
             textBox1_waterDepthUsedValues->Text = HERE;
         }
};
}

したがって、Form2のtextBox(Used_values)に表示されるのは2170(Form1のtextBoxのデフォルト値)です。

4

2 に答える 2

1

のコンストラクタで参照を渡しますUsed_values

#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 Vp_vs_gasconcentration_WindowsForm {

public ref class Used_values : public System::Windows::Forms::Form
{
public:
    Used_values(System::String^ here)
    {
        InitializeComponent();
        HERE = here;
    }

protected:
    ~Used_values()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::TextBox^  textBox1_waterDepthUsedValues;
         System::String^ HERE;
//(more code...)

private:
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    void InitializeComponent(void)
    {
        //(more code...)
        this->textBox1_waterDepthUsedValues = (gcnew System::Windows::Forms::TextBox());
        (more code...)
        // 
        // textBox1_waterDepthUsedValues
        // 
        this->textBox1_waterDepthUsedValues->Location = System::Drawing::Point(435, 10);
        this->textBox1_waterDepthUsedValues->Name = L"textBox1_waterDepthUsedValues";
        this->textBox1_waterDepthUsedValues->Size = System::Drawing::Size(100, 25);
        this->textBox1_waterDepthUsedValues->TabIndex = 1;
        this->textBox1_waterDepthUsedValues->TextAlign = 
        System::Windows::Forms::HorizontalAlignment::Right;
        this->textBox1_waterDepthUsedValues->TextChanged += 
        gcnew System::EventHandler(this, &Used_values::textBox1_waterDepthUsedValues_TextChanged);
        //(more code...)
    }
#pragma endregion

 private: System::Void textBox1_waterDepthUsedValues_TextChanged(System::Object^  sender, System::EventArgs^  e) {
         textBox1_waterDepthUsedValues->Text = HERE;
     }
};
}


Used_values(System::String^ here)
    {
        InitializeComponent();
        String ^HERE = here;
    }

voidそして、引数として使用しないでください、これはCスタイルです!

編集

#pragma once
#include "Used_values.h"

namespace Vp_vs_gasconcentration_WindowsForm {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1()
    {
        InitializeComponent();
    }

protected:
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::MenuStrip^  menuStrip1_generalMenu;
private: System::Windows::Forms::ToolStripMenuItem^  usedValues_ToolStripMenuItem;
private: System::Windows::Forms::TextBox^  textBox1_waterDepth;
//(more code...)

private:
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    void InitializeComponent(void)
    {
        //(more code...)
        this->textBox1_waterDepth = (gcnew System::Windows::Forms::TextBox());
        //(more code...)
        // 
        // textBox1_waterDepth
        // 
        this->textBox1_waterDepth->Font = (gcnew System::Drawing::Font(L"Segoe UI", 10));
        this->textBox1_waterDepth->Location = System::Drawing::Point(336, 185);
        this->textBox1_waterDepth->Name = L"textBox1_waterDepth";
        this->textBox1_waterDepth->Size = System::Drawing::Size(100, 25);
        this->textBox1_waterDepth->TabIndex = 1;
        this->textBox1_waterDepth->Text = L"2170";
        this->textBox1_waterDepth->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
        this->textBox1_waterDepth->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_waterDepth_TextChanged);
        //(more code...)
    }

#pragma endregion
//(more code...)
private: System::Void usedValues_ToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
             Used_values^ used_values = gcnew Used_values();
             used_values->StartPosition = FormStartPosition::CenterParent;
             used_values->ShowDialog();
             used_values->HERE = textBox1_waterDepth->Text;
         }
//(more code...)
};
}

Used_values(子):

#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 Vp_vs_gasconcentration_WindowsForm {

public ref class Used_values : public System::Windows::Forms::Form
{
public:
    Used_values()
    {
        InitializeComponent();
    }

String ^HERE; //declared HERE as public


protected:
    ~Used_values()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::TextBox^  textBox1_waterDepthUsedValues;
//(more code...)

private:
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    void InitializeComponent(void)
    {
        //(more code...)
        this->textBox1_waterDepthUsedValues = (gcnew System::Windows::Forms::TextBox());
  (more code...)
        // 
        // textBox1_waterDepthUsedValues
        // 
        this->textBox1_waterDepthUsedValues->Location = System::Drawing::Point(435, 10);
        this->textBox1_waterDepthUsedValues->Name = L"textBox1_waterDepthUsedValues";
        this->textBox1_waterDepthUsedValues->Size = System::Drawing::Size(100, 25);
        this->textBox1_waterDepthUsedValues->TabIndex = 1;
        this->textBox1_waterDepthUsedValues->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
        this->textBox1_waterDepthUsedValues->TextChanged += gcnew System::EventHandler(this, &Used_values::textBox1_waterDepthUsedValues_TextChanged);
        //(more code...)
    }
 #pragma endregion

private: System::Void textBox1_waterDepthUsedValues_TextChanged(System::Object^  sender, System::EventArgs^  e) {
         textBox1_waterDepthUsedValues->Text = HERE;
     }
};
}

これに応じて内容を変更しました

于 2013-03-26T11:01:36.710 に答える
0

ついに私は解決策を見つけました!すべてのサポートを提供してくれたbash.dに感謝します=)

  1. 文字列パラメーターを受け取るように子フォームのコンストラクターを変更します。

    public:
        Form2(String^ text)
        {
            InitializeComponent();
            this->textBox2->Text = text;
        }
    
  2. 親フォームでボタンがクリックされたときに子フォームを表示します。

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
         Form2^ form = gcnew Form2(this->textBox1->Text);
         form->Show();
     }
    
于 2013-04-04T08:36:29.007 に答える