Solved

Display content of an ArrayList in a listbox

Posted on 2010-09-11
5
546 Views
Last Modified: 2013-12-17
Hi Experts,

I am running a very simple program that must get input from a couple of textboxes and store that as a record in an ArrayList and later I want to display every record in a message box and in list box.
The problem I am having is that I am unable to show all record in the ArrayList. Only the last record gets to be display a number of time equivalent to the number of items contained in the ArrayList.
What am I doing wrong?
Below is the code I am using.

Thanks.

public partial class TheForm : Form

    {

        Person myPerson = new Person();

        ArrayList Friends = new ArrayList();



        public TheForm()

        {

            InitializeComponent();

        }



        private void btnExit_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }



        private void btnGetName_Click(object sender, EventArgs e)

        {

            myPerson.FirstName = txtFirstName.Text;

            myPerson.LastName = txtLastName.Text;



            MessageBox.Show("The name entered is " + myPerson.FirstName + " " + myPerson.LastName);

            Friends.Add(myPerson);

            

            txtFirstName.Text = "";

            txtLastName.Text = "";



        }



        private void btnShowContent_Click(object sender, EventArgs e)

        {

            //listBoxFriends.DataSource = Friends;

                                    

            foreach (Person myPerson in Friends)

            {

                listBoxFriends.Items.Add(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString());

                MessageBox.Show(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString() );

                

            }



        }

    }

Open in new window

0
Comment
Question by:Smanyx
  • 3
5 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33653669
this should work
public partial class TheForm : Form

    {

        ArrayList Friends = new ArrayList();



        public TheForm()

        {

            InitializeComponent();

        }



        private void btnExit_Click(object sender, EventArgs e)

        {

            Application.Exit();

        }



        private void btnGetName_Click(object sender, EventArgs e)

        {

var myPerson = new Person();

            myPerson.FirstName = txtFirstName.Text;

            myPerson.LastName = txtLastName.Text;



            MessageBox.Show("The name entered is " + myPerson.FirstName + " " + myPerson.LastName);

            Friends.Add(myPerson);

            

            txtFirstName.Text = "";

            txtLastName.Text = "";



        }



        private void btnShowContent_Click(object sender, EventArgs e)

        {

            //listBoxFriends.DataSource = Friends;

                                    

            foreach (Person myPerson in Friends)

            {

                listBoxFriends.Items.Add(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString());

                MessageBox.Show(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString() );

                

            }



        }

    }

Open in new window

0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 125 total points
ID: 33653681
the problem was the myperson object u declared was global, so when u change it to add a new name the one in the array will also reflect the change...
0
 

Expert Comment

by:RAJESH SHARMA
ID: 33653719
Try This.................

public partial class TheForm : Form
    {
       //  Person myPerson = new Person(); // Remove this line
        ArrayList Friends = new ArrayList();

        public TheForm()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnGetName_Click(object sender, EventArgs e)
        {
        // Create an object of person
           Person myPerson = new Person();

            myPerson.FirstName = txtFirstName.Text;
            myPerson.LastName = txtLastName.Text;

            MessageBox.Show("The name entered is " + myPerson.FirstName + " " + myPerson.LastName);
            Friends.Add(myPerson);
           
            txtFirstName.Text = "";
            txtLastName.Text = "";

        }

        private void btnShowContent_Click(object sender, EventArgs e)
        {
            //listBoxFriends.DataSource = Friends;
                                   
            foreach (Person myPerson in Friends)
            {
                listBoxFriends.Items.Add(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString());
                MessageBox.Show(myPerson.FirstName.ToString() + " " + myPerson.LastName.ToString() );
               
            }

        }
    }
0
 

Author Closing Comment

by:Smanyx
ID: 33653720
Thanks, it works.

Just out of curiosity, why have you used var myPerson and not Person myPerson as I did?
Cheers.
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 33653793
just lazy i guess :-)... there is no difference between these two lines

Person myPerson = new Person();

and

var myPerson = new Person();

in case of var the type is inferred by what you write on the rhs.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now