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.
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() );
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(objec t sender, EventArgs e)
{
//listBoxFriends.DataSourc e = Friends;
foreach (Person myPerson in Friends)
{
listBoxFriends.Items.Add(m yPerson.Fi rstName.To String() + " " + myPerson.LastName.ToString ());
MessageBox.Show(myPerson.F irstName.T oString() + " " + myPerson.LastName.ToString () );
}
}
}
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(objec
{
//listBoxFriends.DataSourc
foreach (Person myPerson in Friends)
{
listBoxFriends.Items.Add(m
MessageBox.Show(myPerson.F
}
}
}
ASKER
Thanks, it works.
Just out of curiosity, why have you used var myPerson and not Person myPerson as I did?
Cheers.
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.
Person myPerson = new Person();
and
var myPerson = new Person();
in case of var the type is inferred by what you write on the rhs.
Open in new window