L
lukasmazur
Hi
I have a problem with using listBox1. I have a two forms form1 and
form2. In form1 are controls listBox1, textBox1 and button witch
creating object of class Form2. In class Form2 I create a pointer to
object of class Form1. I don't known how to use method add(), where
can I find it. From Form1 I can add value like this this->listBox1-
control like this fm1->Controls["textBox1"]->Text = "text in box"; .
But Text is property of class not method like add().
How can I use add() method from the Form2 to listBox in another form?
VS2005Pro, .net Framework 2.0
Below I attach the listening
#Form1##########################################################
#pragma once
#include"Form2.h"
namespace naforum {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
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;
public: System::Windows::Forms::ListBox^ listBox1;
private:
public: System::Windows::Forms::TextBox^ textBox1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System:rawing:oint(173, 12);
this->button1->Name = L"button1";
this->button1->Size = System:rawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->Location = System:rawing:oint(16, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System:rawing::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System:rawing:oint(16, 113);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System:rawing::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimensions = System:rawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System:rawing::Size(292, 273);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->listBox1);
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 ^fm2 = gcnew Form2();
fm2->Show();
this->listBox1->Items[1]->Add("aaaa"); //from this class method
is available
}
};
}
#Form2#####################################################################
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
namespace naforum {
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^ button1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System:rawing:oint(12, 22);
this->button1->Name = L"button1";
this->button1->Size = System:rawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System:rawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System:rawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
Form ^fm1 = Application::OpenForms["Form1"];
fm1->Text = "text"; //it is work
fm1->Controls["textBox1"]->Text = "text in box"; //is ok
//fm1->Controls["listBox1"]->... what is next ?????
}
};
}
I have a problem with using listBox1. I have a two forms form1 and
form2. In form1 are controls listBox1, textBox1 and button witch
creating object of class Form2. In class Form2 I create a pointer to
object of class Form1. I don't known how to use method add(), where
can I find it. From Form1 I can add value like this this->listBox1-
I cant find it. I have textBox1 on Form1 and I can change text in thisItems[1]->Add("aaaa"); but from Form2 when I try to get this method
control like this fm1->Controls["textBox1"]->Text = "text in box"; .
But Text is property of class not method like add().
How can I use add() method from the Form2 to listBox in another form?
VS2005Pro, .net Framework 2.0
Below I attach the listening
#Form1##########################################################
#pragma once
#include"Form2.h"
namespace naforum {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
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;
public: System::Windows::Forms::ListBox^ listBox1;
private:
public: System::Windows::Forms::TextBox^ textBox1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System:rawing:oint(173, 12);
this->button1->Name = L"button1";
this->button1->Size = System:rawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = true;
this->listBox1->Location = System:rawing:oint(16, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System:rawing::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System:rawing:oint(16, 113);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System:rawing::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimensions = System:rawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System:rawing::Size(292, 273);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->listBox1);
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 ^fm2 = gcnew Form2();
fm2->Show();
this->listBox1->Items[1]->Add("aaaa"); //from this class method
is available
}
};
}
#Form2#####################################################################
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
namespace naforum {
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^ button1;
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->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System:rawing:oint(12, 22);
this->button1->Name = L"button1";
this->button1->Size = System:rawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System:rawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System:rawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
Form ^fm1 = Application::OpenForms["Form1"];
fm1->Text = "text"; //it is work
fm1->Controls["textBox1"]->Text = "text in box"; //is ok
//fm1->Controls["listBox1"]->... what is next ?????
}
};
}