I need to show the value of textBox1 of Form1 (parent) in textBox2 of Form2 (child) after pressing an option in a menu (ie. to open the child form). Basically a parent-child communication problem. I've tried to modify this to do what I need but no luck, I get:
Used_values.h(30) : warning C4101: 'HERE' : unreferenced local variable
Used_values.h(560) : error C2065: 'HERE' : undeclared identifier
Here you can find the relevant parts of my code:
Form1 (parent):
#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;
}
};
}
So, what it should appear in textBox of Form2 (Used_values) is 2170 (default value of textBox in Form1).
Finally I found the solution! thanks to bash.d for all the support =)
change in the constructor of child form to receive a String parameter:
public:
Form2(String^ text)
{
InitializeComponent();
this->textBox2->Text = text;
}
show child form when a button is clicked in parent form:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form2^ form = gcnew Form2(this->textBox1->Text);
form->Show();
}