Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lukasmazur@gmail.com - 24 Nov 2007 17:04 GMT
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 ?????

            }
    };
}
Peteroid - 24 Nov 2007 17:36 GMT
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.

> Hi
>
[quoted text clipped - 210 lines]
> };
> }
Peteroid - 24 Nov 2007 17:41 GMT
Woops, my bad. Just noticed you also attached a listing, and indeed the
controls in question are defined as public in Form1.

> The controls listBox1 and textBox1 are PRIVATE to class Form1. Hence their
> members are inaccessible to instances of class Form2. From inside the
[quoted text clipped - 220 lines]
>> };
>> }
lukasmazur@gmail.com - 24 Nov 2007 18:40 GMT
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".

> Woops, my bad. Just noticed you also attached a listing, and indeed the
> controls in question are defined as public in Form1.
[quoted text clipped - 225 lines]
> >> };
> >> }
lukasmazur@gmail.com - 24 Nov 2007 18:40 GMT
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".

> Woops, my bad. Just noticed you also attached a listing, and indeed the
> controls in question are defined as public in Form1.
[quoted text clipped - 225 lines]
> >> };
> >> }
lukasmazur@gmail.com - 24 Nov 2007 19:04 GMT
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;
>
[quoted text clipped - 238 lines]
> > >> };
> > >> }

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");
       }
   }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.