Solved

Display content of an ArrayList in a listbox

Posted on 2010-09-11
5
549 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 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