VC ++:form1的textBox值必须出现在form2的textBox中

时间:2013-03-26 10:57:45

标签: winforms visual-studio visual-c++ textbox c++-cli

很抱歉,如果这对某些人来说是重复的,但在搜索后我找不到适合我的答案。我需要在菜单中按下一个选项(即打开子表单)后,在Form2(子)的textBox2中显示Form1(父)的textBox1的值。基本上是亲子沟通问题。请光临我,我是CLI / C ++的新手(VC ++ 2008)。我试图修改this来做我需要但没有运气,我得到:

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(child):

#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的默认值)。

非常欢迎任何想法和提示解决这个问题,提前谢谢,

2 个答案:

答案 0 :(得分: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(child):

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

根据this更改了内容。

答案 1 :(得分:0)

最后我找到了解决方案!感谢bash.d所有的支持=)

  1. 更改子窗体的构造函数以接收String参数:

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