473,508 Members | 2,360 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::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();
//

//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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = 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->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::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(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::Data;
using namespace System::Drawing;
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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::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 ?????

}
};
}
Nov 24 '07 #1
5 2497
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********@gmail.comwrote in message
news:d8**********************************@d4g2000p rg.googlegroups.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::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();
//

//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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = 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->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::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(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::Data;
using namespace System::Drawing;
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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::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 ?????

}
};
}
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********@hotmail.comwrote in message
news:07**********************************@microsof t.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********@gmail.comwrote in message
news:d8**********************************@d4g2000p rg.googlegroups.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::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();
//

//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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = 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->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::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(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::Data;
using namespace System::Drawing;
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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::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 ?????

}
};
}
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...@hotmail.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...@hotmail.comwrote in message

news:07**********************************@microsof t.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...@gmail.comwrote in message
news:d8**********************************@d4g2000p rg.googlegroups.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::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();
//
//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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = 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->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::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(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::Data;
using namespace System::Drawing;
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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::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 ?????
}
};
}
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...@hotmail.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...@hotmail.comwrote in message

news:07**********************************@microsof t.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...@gmail.comwrote in message
news:d8**********************************@d4g2000p rg.googlegroups.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::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();
//
//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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form1::button1_Click);
//
// listBox1
//
this->listBox1->FormattingEnabled = 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->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::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(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::Data;
using namespace System::Drawing;
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::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->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,
&Form2::button1_Click);
//
// Form2
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::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 ?????
}
};
}
Nov 24 '07 #5
On 24 Lis, 19:40, lukasma...@gmail.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...@hotmail.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...@hotmail.comwrote in message
news:07**********************************@microsof t.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...@gmail.comwrote in message
>news:d8**********************************@d4g2000 prg.googlegroups.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::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();
>//
>//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::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->UseVisualStyleBackColor = true;
>this->button1->Click += gcnew System::EventHandler(this,
>&Form1::button1_Click);
>//
>// listBox1
>//
>this->listBox1->FormattingEnabled = 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->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
>this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::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(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::Data;
>using namespace System::Drawing;
>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::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->UseVisualStyleBackColor = true;
>this->button1->Click += gcnew System::EventHandler(this,
>&Form2::button1_Click);
>//
>// Form2
>//
>this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
>this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
>this->ClientSize = System::Drawing::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 ?????
>}
>};
>}


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

Below listening:

#Form1############################################ ##
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

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

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

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

private void button1_Click(object sender, EventArgs e)
{

Form1 form1 =(Form1)Application.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
2547
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...
5
3361
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...
1
3067
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....
0
1614
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) {...
2
21342
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...
5
1845
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...
3
2466
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...
1
3462
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...
0
6629
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...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7321
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,...
0
7377
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...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5047
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...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
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...

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.