Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Display content of an ArrayList in a listbox

Posted on 2010-09-11
5
Medium Priority
?
554 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

783 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