473,898 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I use add() method from the Form2 to listBox in another form?

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-
>Items[1]->Add("aaaa"); but from Form2 when I try to get this method
I cant find it. I have textBox1 on Form1 and I can change text in this
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::Compone ntModel;
using namespace System::Collect ions;
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)
{
InitializeCompo nent();
//

//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::ListBo x^ listBox1;
private:
public: System::Windows ::Forms::TextBo x^ textBox1;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->listBox1 = (gcnew System::Windows ::Forms::ListBo x());
this->textBox1 = (gcnew System::Windows ::Forms::TextBo x());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(173, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form1::button1 _Click);
//
// listBox1
//
this->listBox1->FormattingEnab led = true;
this->listBox1->Location = System::Drawing ::Point(16, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing ::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System::Drawing ::Point(16, 113);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing ::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::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(f alse);
this->PerformLayout( );

}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ 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::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;
namespace naforum {

public ref class Form2 : public System::Windows ::Forms::Form
{
public:
Form2(void)
{
InitializeCompo nent();
//
//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::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(12, 22);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form2::button1 _Click);
//
// Form2
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(f alse);

}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ e) {
Form ^fm1 = Application::Op enForms["Form1"];
fm1->Text = "text"; //it is work
fm1->Controls["textBox1"]->Text = "text in box"; //is ok
//fm1->Controls["listBox1"]->... what is next ?????

}
};
}
Nov 24 '07 #1
5 2539
The controls listBox1 and textBox1 are PRIVATE to class Form1. Hence their
members are inaccessible to instances of class Form2. From inside the
definition of Form1 all its controls are accessible, private or not, so
within Form1 they are 'findable'.

The solution to making the controls you want in Form1 accessible to Form2
(and hence their public methods, such as Add), is making them PUBLIC within
the definition of class Form1 or by writing methods in class Form1 that
expose them in a second-hand way.

<lu********@gma il.comwrote in message
news:d8******** *************** ***********@d4g 2000prg.googleg roups.com...
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-
>>Items[1]->Add("aaaa"); but from Form2 when I try to get this method
I cant find it. I have textBox1 on Form1 and I can change text in this
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::Compone ntModel;
using namespace System::Collect ions;
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)
{
InitializeCompo nent();
//

//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::ListBo x^ listBox1;
private:
public: System::Windows ::Forms::TextBo x^ textBox1;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->listBox1 = (gcnew System::Windows ::Forms::ListBo x());
this->textBox1 = (gcnew System::Windows ::Forms::TextBo x());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(173, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form1::button1 _Click);
//
// listBox1
//
this->listBox1->FormattingEnab led = true;
this->listBox1->Location = System::Drawing ::Point(16, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing ::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System::Drawing ::Point(16, 113);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing ::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::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(f alse);
this->PerformLayout( );

}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ 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::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;
namespace naforum {

public ref class Form2 : public System::Windows ::Forms::Form
{
public:
Form2(void)
{
InitializeCompo nent();
//
//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::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(12, 22);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form2::button1 _Click);
//
// Form2
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(f alse);

}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ e) {
Form ^fm1 = Application::Op enForms["Form1"];
fm1->Text = "text"; //it is work
fm1->Controls["textBox1"]->Text = "text in box"; //is ok
//fm1->Controls["listBox1"]->... what is next ?????

}
};
}
Nov 24 '07 #2
Woops, my bad. Just noticed you also attached a listing, and indeed the
controls in question are defined as public in Form1.

"Peteroid" <pe********@hot mail.comwrote in message
news:07******** *************** ***********@mic rosoft.com...
The controls listBox1 and textBox1 are PRIVATE to class Form1. Hence their
members are inaccessible to instances of class Form2. From inside the
definition of Form1 all its controls are accessible, private or not, so
within Form1 they are 'findable'.

The solution to making the controls you want in Form1 accessible to Form2
(and hence their public methods, such as Add), is making them PUBLIC
within the definition of class Form1 or by writing methods in class Form1
that expose them in a second-hand way.

<lu********@gma il.comwrote in message
news:d8******** *************** ***********@d4g 2000prg.googleg roups.com...
>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-
>>>Items[1]->Add("aaaa"); but from Form2 when I try to get this method
I cant find it. I have textBox1 on Form1 and I can change text in this
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::Compone ntModel;
using namespace System::Collect ions;
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)
{
InitializeComp onent();
//

//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::ListBo x^ listBox1;
private:
public: System::Windows ::Forms::TextBo x^ textBox1;
protected:

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::Compon entModel::Conta iner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->listBox1 = (gcnew System::Windows ::Forms::ListBo x());
this->textBox1 = (gcnew System::Windows ::Forms::TextBo x());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(173, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form1::button 1_Click);
//
// listBox1
//
this->listBox1->FormattingEnab led = true;
this->listBox1->Location = System::Drawing ::Point(16, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing ::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System::Drawing ::Point(16, 113);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing ::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::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(f alse);
this->PerformLayout( );

}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventA rgs^ 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::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;
namespace naforum {

public ref class Form2 : public System::Windows ::Forms::Form
{
public:
Form2(void)
{
InitializeComp onent();
//
//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::Compon entModel::Conta iner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(12, 22);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form2::button 1_Click);
//
// Form2
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(f alse);

}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventA rgs^ e) {
Form ^fm1 = Application::Op enForms["Form1"];
fm1->Text = "text"; //it is work
fm1->Controls["textBox1"]->Text = "text in box"; //is ok
//fm1->Controls["listBox1"]->... what is next ?????

}
};
}
Nov 24 '07 #3
I have access to form because I can change value of textBox: fm1-
>Controls["textBox1"]->Text;
But I don't know how I can use method add() from another form fm1-
>Controls["listBox1"]->... what is next ?????
I tray do this similar like this->listBox1->Items->Add("aaaa"); But
when I try do this using pointer I cant find it : fm1-
>Controls["listBox1"]-(what I mast do next??), there is not option
"Items".

On 24 Lis, 18:41, "Peteroid" <peteroi...@hot mail.comwrote:
Woops, my bad. Just noticed you also attached a listing, and indeed the
controls in question are defined as public in Form1.

"Peteroid" <peteroi...@hot mail.comwrote in message

news:07******** *************** ***********@mic rosoft.com...
The controls listBox1 and textBox1 are PRIVATE to class Form1. Hence their
members are inaccessible to instances of class Form2. From inside the
definition of Form1 all its controls are accessible, private or not, so
within Form1 they are 'findable'.
The solution to making the controls you want in Form1 accessible to Form2
(and hence their public methods, such as Add), is making them PUBLIC
within the definition of class Form1 or by writing methods in class Form1
that expose them in a second-hand way.
<lukasma...@gma il.comwrote in message
news:d8******** *************** ***********@d4g 2000prg.googleg roups.com...
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-
Items[1]->Add("aaaa"); but from Form2 when I try to get this method
I cant find it. I have textBox1 on Form1 and I can change text in this
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::Compone ntModel;
using namespace System::Collect ions;
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)
{
InitializeCompo nent();
//
//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::ListBo x^ listBox1;
private:
public: System::Windows ::Forms::TextBo x^ textBox1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->listBox1 = (gcnew System::Windows ::Forms::ListBo x());
this->textBox1 = (gcnew System::Windows ::Forms::TextBo x());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(173, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form1::button1 _Click);
//
// listBox1
//
this->listBox1->FormattingEnab led = true;
this->listBox1->Location = System::Drawing ::Point(16, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing ::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System::Drawing ::Point(16, 113);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing ::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::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(f alse);
this->PerformLayout( );
}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ 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::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;
namespace naforum {
public ref class Form2 : public System::Windows ::Forms::Form
{
public:
Form2(void)
{
InitializeCompo nent();
//
//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::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(12, 22);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form2::button1 _Click);
//
// Form2
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(f alse);
}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ e) {
Form ^fm1 = Application::Op enForms["Form1"];
fm1->Text = "text"; //it is work
fm1->Controls["textBox1"]->Text = "text in box"; //is ok
//fm1->Controls["listBox1"]->... what is next ?????
}
};
}
Nov 24 '07 #4
I have access to form because I can change value of textBox: fm1-
>Controls["textBox1"]->Text;
But I don't know how I can use method add() from another form fm1-
>Controls["listBox1"]->... what is next ?????
I tray do this similar like this->listBox1->Items->Add("aaaa"); But
when I try do this using pointer I cant find it : fm1-
>Controls["listBox1"]-(what I mast do next??), there is not option
"Items".

On 24 Lis, 18:41, "Peteroid" <peteroi...@hot mail.comwrote:
Woops, my bad. Just noticed you also attached a listing, and indeed the
controls in question are defined as public in Form1.

"Peteroid" <peteroi...@hot mail.comwrote in message

news:07******** *************** ***********@mic rosoft.com...
The controls listBox1 and textBox1 are PRIVATE to class Form1. Hence their
members are inaccessible to instances of class Form2. From inside the
definition of Form1 all its controls are accessible, private or not, so
within Form1 they are 'findable'.
The solution to making the controls you want in Form1 accessible to Form2
(and hence their public methods, such as Add), is making them PUBLIC
within the definition of class Form1 or by writing methods in class Form1
that expose them in a second-hand way.
<lukasma...@gma il.comwrote in message
news:d8******** *************** ***********@d4g 2000prg.googleg roups.com...
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-
Items[1]->Add("aaaa"); but from Form2 when I try to get this method
I cant find it. I have textBox1 on Form1 and I can change text in this
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::Compone ntModel;
using namespace System::Collect ions;
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)
{
InitializeCompo nent();
//
//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::ListBo x^ listBox1;
private:
public: System::Windows ::Forms::TextBo x^ textBox1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->listBox1 = (gcnew System::Windows ::Forms::ListBo x());
this->textBox1 = (gcnew System::Windows ::Forms::TextBo x());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(173, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form1::button1 _Click);
//
// listBox1
//
this->listBox1->FormattingEnab led = true;
this->listBox1->Location = System::Drawing ::Point(16, 12);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing ::Size(120, 95);
this->listBox1->TabIndex = 1;
//
// textBox1
//
this->textBox1->Location = System::Drawing ::Point(16, 113);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing ::Size(100, 20);
this->textBox1->TabIndex = 2;
//
// Form1
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::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(f alse);
this->PerformLayout( );
}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ 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::Compone ntModel;
using namespace System::Collect ions;
using namespace System::Windows ::Forms;
using namespace System::Data;
using namespace System::Drawing ;
namespace naforum {
public ref class Form2 : public System::Windows ::Forms::Form
{
public:
Form2(void)
{
InitializeCompo nent();
//
//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::Compone ntModel::Contai ner ^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 InitializeCompo nent(void)
{
this->button1 = (gcnew System::Windows ::Forms::Button ());
this->SuspendLayout( );
//
// button1
//
this->button1->Location = System::Drawing ::Point(12, 22);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing ::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyle BackColor = true;
this->button1->Click += gcnew System::EventHa ndler(this,
&Form2::button1 _Click);
//
// Form2
//
this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
this->ClientSize = System::Drawing ::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form2";
this->Text = L"Form2";
this->ResumeLayout(f alse);
}
#pragma endregion
private: System::Void button1_Click(S ystem::Object^ sender,
System::EventAr gs^ e) {
Form ^fm1 = Application::Op enForms["Form1"];
fm1->Text = "text"; //it is work
fm1->Controls["textBox1"]->Text = "text in box"; //is ok
//fm1->Controls["listBox1"]->... what is next ?????
}
};
}
Nov 24 '07 #5
On 24 Lis, 19:40, lukasma...@gmai l.com wrote:
I have access to form because I can change value of textBox: fm1->Controls["textBox1"]->Text;

But I don't know how I can use method add() from another form fm1->Controls["listBox1"]->... what is next ?????

I tray do this similar like this->listBox1->Items->Add("aaaa"); But
when I try do this using pointer I cant find it : fm1->Controls["listBox1"]-(what I mast do next??), there is not option

"Items".

On 24 Lis, 18:41, "Peteroid" <peteroi...@hot mail.comwrote:
Woops, my bad. Just noticed you also attached a listing, and indeed the
controls in question are defined as public in Form1.
"Peteroid" <peteroi...@hot mail.comwrote in message
news:07******** *************** ***********@mic rosoft.com...
The controls listBox1 and textBox1 are PRIVATE to class Form1. Hence their
members are inaccessible to instances of class Form2. From inside the
definition of Form1 all its controls are accessible, private or not, so
within Form1 they are 'findable'.
The solution to making the controls you want in Form1 accessible to Form2
(and hence their public methods, such as Add), is making them PUBLIC
within the definition of class Form1 or by writing methods in class Form1
that expose them in a second-hand way.
<lukasma...@gma il.comwrote in message
>news:d8******* *************** ************@d4 g2000prg.google groups.com...
>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-
>>>Items[1]->Add("aaaa"); but from Form2 when I try to get this method
>I cant find it. I have textBox1 on Form1 and I can change text in this
>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::Compone ntModel;
>using namespace System::Collect ions;
>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)
>{
>InitializeComp onent();
>//
>//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::ListBo x^ listBox1;
>private:
>public: System::Windows ::Forms::TextBo x^ textBox1;
>protected:
>private:
>/// <summary>
>/// Required designer variable.
>/// </summary>
>System::Compon entModel::Conta iner ^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 InitializeCompo nent(void)
>{
>this->button1 = (gcnew System::Windows ::Forms::Button ());
>this->listBox1 = (gcnew System::Windows ::Forms::ListBo x());
>this->textBox1 = (gcnew System::Windows ::Forms::TextBo x());
>this->SuspendLayout( );
>//
>// button1
>//
>this->button1->Location = System::Drawing ::Point(173, 12);
>this->button1->Name = L"button1";
>this->button1->Size = System::Drawing ::Size(75, 23);
>this->button1->TabIndex = 0;
>this->button1->Text = L"button1";
>this->button1->UseVisualStyle BackColor = true;
>this->button1->Click += gcnew System::EventHa ndler(this,
>&Form1::button 1_Click);
>//
>// listBox1
>//
>this->listBox1->FormattingEnab led = true;
>this->listBox1->Location = System::Drawing ::Point(16, 12);
>this->listBox1->Name = L"listBox1";
>this->listBox1->Size = System::Drawing ::Size(120, 95);
>this->listBox1->TabIndex = 1;
>//
>// textBox1
>//
>this->textBox1->Location = System::Drawing ::Point(16, 113);
>this->textBox1->Name = L"textBox1";
>this->textBox1->Size = System::Drawing ::Size(100, 20);
>this->textBox1->TabIndex = 2;
>//
>// Form1
>//
>this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
>this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
>this->ClientSize = System::Drawing ::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(f alse);
>this->PerformLayout( );
>}
>#pragma endregion
>private: System::Void button1_Click(S ystem::Object^ sender,
>System::EventA rgs^ 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::Compone ntModel;
>using namespace System::Collect ions;
>using namespace System::Windows ::Forms;
>using namespace System::Data;
>using namespace System::Drawing ;
>namespace naforum {
>public ref class Form2 : public System::Windows ::Forms::Form
>{
>public:
>Form2(void)
>{
>InitializeComp onent();
>//
>//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::Compon entModel::Conta iner ^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 InitializeCompo nent(void)
>{
>this->button1 = (gcnew System::Windows ::Forms::Button ());
>this->SuspendLayout( );
>//
>// button1
>//
>this->button1->Location = System::Drawing ::Point(12, 22);
>this->button1->Name = L"button1";
>this->button1->Size = System::Drawing ::Size(75, 23);
>this->button1->TabIndex = 0;
>this->button1->Text = L"button1";
>this->button1->UseVisualStyle BackColor = true;
>this->button1->Click += gcnew System::EventHa ndler(this,
>&Form2::button 1_Click);
>//
>// Form2
>//
>this->AutoScaleDimen sions = System::Drawing ::SizeF(6, 13);
>this->AutoScaleMod e = System::Windows ::Forms::AutoSc aleMode::Font;
>this->ClientSize = System::Drawing ::Size(292, 273);
>this->Controls->Add(this->button1);
>this->Name = L"Form2";
>this->Text = L"Form2";
>this->ResumeLayout(f alse);
>}
>#pragma endregion
>private: System::Void button1_Click(S ystem::Object^ sender,
>System::EventA rgs^ e) {
>Form ^fm1 = Application::Op enForms["Form1"];
>fm1->Text = "text"; //it is work
>fm1->Controls["textBox1"]->Text = "text in box"; //is ok
>//fm1->Controls["listBox1"]->... what is next ?????
>}
>};
>}


When I try do this similar in c# I made this easily.

Below listening:

#Form1######### ############### ############### #######
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace chas
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
}

#Form2######### ############### ############### ############### ######

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace chas
{
public partial class Form2 : Form
{
public Form2()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{

Form1 form1 =(Form1)Applica tion.OpenForms["Form1"];
form1.listBox1. Items.Add("one record in listBox");
}
}
}
Nov 24 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2561
by: Kingdom | last post by:
I Need some serious help here. strugling novis with ASP and javascript any help would be greatly appreciated The script below does exactly what I want it to do for each product on the two passes it makes however I would like the entire script to loop 34 times and on each of those 30 loops also write the product and the price (only) onto the page building a compleate list of all the products selected over the 34 loops ending with a total...
5
3377
by: Bob | last post by:
Hi Everybody I have a form called frmListBox that is connected to a table tblListBox. This is opened from a form called "frmInvoiceOrder" which has a subform called "zfrmInvoiceOrder" and floats above (ie is pop-up) ! Private Sub ListBox_DblClick(Cancel As Integer) DoCmd.OpenForm "Forms!!!.Form! = Me!ListBox.Column(1) Forms!!.Form! = Me!ListBox.Column(2)
1
3096
by: Mickey Swanson | last post by:
I have a MDI app wich has several different forms I need to call a method on one MDIchild form from another child form. I have a MDI form called frmMDI. I have a mdichild form called frmMain. I have a mdichild form called frmDetails. after saving changes to frmDetails I need to update frmMain
0
1645
by: Brian | last post by:
Create a simple MDI form and a child form, and try the following simple code to add a new menu item on the Edit menu popup: private void editMenu_Popup(object sender, System.EventArgs e) { editMoreMenu.MenuItems.Add("New MI " + count); count++; } and get "An unhandled exception of type 'System.NullReferenceException'
2
21354
by: Galaxia | last post by:
I am trying to add a Form into a Panel in .NET 2003: Form2 ff2 = new Form2(); panel2.Controls.Add(ff2); But it failed in runtime which said I could not add a top control into another control. Can I have any way to add a Form into a Panel? Need I modify any property of the Form to make it not a "top control"? -- "Forward Galaxia"
5
1860
by: PAPutzback | last post by:
Form2 has one purpose to open and list some names and ids. I want to handle the list box click event on form2 so I can get the selected value onto a field in form1. I changed this Dim MyForm2 As New PHfx.Form2 to With Events MyForm2 As New PHfx.Form2 and tried adding an event handler but intellisense will not show form2s objects, just the methods.
3
2485
by: R. Harris | last post by:
Hi. I have 2 forms: form1 form2 On Form2 I have a listbox and a button. When I click the button it calls a function from form1 and within that function it updates the listbox on form2. My problem is I don't see the items added to the listbox unless I use
1
3501
by: pgreenhill | last post by:
Hi All, I'm creating a Form ("Menu") that has 2 listboxs in it. My problem is that I can't copy the values of each listbox, into another Form's textboxs. The idea is that users can select their options in the "Menu" form and then open the other form, and have those selected options copied in their respective textboxs. I used this code from a post in this forum, (Discussion: Copy data from one FORM to another FORM): ...
0
6655
by: syntax | last post by:
Hi, I am quite new to C# .NET, trying to understand the MDI Parent / Child form relations.. currently have an issue with regards to adding a child form to a mdi parent split container panel via method inside another child form. The squence of events as follows : MDI Parent MDIMain loaded MDI Child Form1 loaded Form1 should load MDI Child Form2 and adds it to split container panel2 in MDIMain, finally display Form2 inside the MDIMain...
0
9842
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11265
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10859
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10487
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3308
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.