Link to home
Start Free TrialLog in
Avatar of Smanyx
Smanyx

asked on

Display content of an ArrayList in a listbox

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

Avatar of Gautham Janardhan
Gautham Janardhan

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

ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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() );
               
            }

        }
    }
Avatar of Smanyx

ASKER

Thanks, it works.

Just out of curiosity, why have you used var myPerson and not Person myPerson as I did?
Cheers.
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.