Hello, I am writing a program in Visual C++ 10 with 2 forms.
In the first form I have a textbox and a button.
The button opens the second form.
In the second form, I have a button that changes the text in the textbox in form1 to an item from a listbox in form2.
Here is my problem. When I do the code for the button in form2 to change the textbox in form1 I get the errors:
error C2065: 'textBox1' : undeclared identifier
error C2227: left of '->Text' must point to class/struct/union/generic type
I thought maybe I should just include the "Form1.h" file but I can't do that because I already included "Form2.h" in form1 in order to be able to open the second form. If I try to include form1 in form2, it says that the code to open form2 is an error now.
My question is, how can I access identifiers such as "textbox1" from other forms and other files when I already used the first form to open the second form? I also want to know how to do this for all identifiers between all files.
One more question is how to print the selecteditem from a listbox into a textbox because that doesn't work by just setting them equal either.
Thank you. Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#pragma once
#include "Form2.h"
namespace Test1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(155, 60);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(100, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Second Form";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(155, 124);
this->textBox1->Multiline = true;
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 45);
this->textBox1->TabIndex = 1;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(386, 227);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form2^ frm = gcnew Form2;
frm->Show(this);
}
};
}
|
Form2.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#pragma once
namespace Test1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form2
/// </summary>
public ref class Form2 : public System::Windows::Forms::Form
{
public:
Form2(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form2()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button2;
protected:
private: System::Windows::Forms::ListBox^ listBox1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button2 = (gcnew System::Windows::Forms::Button());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->SuspendLayout();
//
// button2
//
this->button2->Location = System::Drawing::Point(75, 152);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(140, 51);
this->button2->TabIndex = 0;
this->button2->Text = L"Add";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form2::button2_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->Items->AddRange(gcnew cli::array< System::Object^ >(3) {L"Option 1", L"Option 2", L"Option 3"});
this->listBox1->Location = System::Drawing::Point(84, 32);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Controls->Add(this->listBox1);
this->Controls->Add(this->button2);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
textBox1 -> Text = listBox1 -> SelectedItem;
Close();
}
};
}
|