472,780 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 2276
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
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
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
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
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
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
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
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
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
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
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.