Arraylist question

G

Guest

//I had a class

public class PERSON
{
PERSON(string LName, string FName, string age)
{
m_last = LName;
m_first = FName;
m_age = age;
}
public string LastName
{
get {return m_last;}
set {m_last = value;}
}
public string FirstName
{
get {return m_first;}
set {m_first = value;}
}
public string PersonAge
{
get {return m_age;}
set {m_age = value;}
}
string m_last;
string m_first;
string age;
}

//In Form1 I do this:

ArrayList CollectionList = new ArrayList();

for(int i = 0; i < Person.Length; i++)
{
CollectionList.Add( new PERSON(LastN, FirstN, PAge));
}

foreach (PERSON per in CollectionList)
{
this.LastcomboBox.Text = per.LastName.ToString();
this.FirstcomboBox.Text = per.FirstName.ToString();
}

//My question is: if I had a list of LastName and FirstName display in a
ComBoBox. How do I display Age in TextBox? when user pick the person
LastName it also show that person age in a TextBox?

Any help are appreciated,
 
B

Bruce Wood

First of all, you have a flaw in your user interface design. If you
have one combo box for last name and another for first name, what's to
stop your user from choosing invalid combinations of first and last
name that don't correspond to any Person on your list?

Even if you did prevent that (and it would be tricky), if you have two
people with the same last name then you're going to display that last
name twice in your Lastcombobox list. Ugly.

In order to fix up that situation, I suggest that you add some methods
to your Person class:

Change Person to implement IComparable:

public class Person : IComparable

this will force you to implement the Compare method:

public int CompareTo(object obj)
{
return CompareTo(obj as Person);
}

public int CompareTo(Person otherPerson)
{
int result;
if (otherPerson == null)
{
// Everything is greater than null
result = 1;
}
else
{
result = this.m_last.CompareTo(otherPerson.m_last);
if (result == 0)
{
result = this.m_first.CompareTo(otherPerson.m_first);
}
if (result == 0)
{
result = this.age.CompareTo(otherPerson.age);
}
return result;
}
}

Now, you can sort your array of Persons by last name then first name:

CollectionList.Sort();

Once you've done that, I would put all of the "Person"s in one combo
box, using a last name / first name display. I would create a new
property in Person:

public string ComboBoxDisplay
{
get { return this.m_last + ", " + this.m_first; }
}

then add that string to the combo box for each Person:

foreach (Person per in CollectionList)
{
this.PersoncomboBox.Items.Add(per.ComboBoxDisplay);
}

Now you have to hook up an event handler to your combo box, and write a
method to say what will happen whenever the user changes the selected
index in the combo box:

this.PersoncomboBox.SelectedIndexChanged += new
System.EventHandler(PersoncomboBox_SelectedIndexChanged);

....

private void PersoncomboBox_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
// Unfortunately, ComboBox doesn't have a Tag attribute, so you
have
// to find the chosen Person by position
int selected = this.PersoncomboBox.SelectedIndex;
if (selected < 0)
{
this.txtAge.Text = "";
}
else
{
Person chosenPerson = (Person)this.CollectionList[selected];
this.txtAge.Text = chosenPerson.PersonAge;
}
}
 
G

Guest

Another way to do this, which would not use a collection is to override the
ToString virtual method in your PERSON class to return:

public override string ToString()
{
return m_last + "," + m_first;
}

Now you can add the instances of person directly to the combobox which will
use the ToString() method to put the display text in the combobox drop down
list. i.e

PERSON p1 = new PERSON("last", "first", 22);
comboBox1.Add(p1);

When you want to get the value back you can simply get the PERSON object
back out of the Combobox by using the SelectedItem property i.e.

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
PERSON selectedPerson = comboBox1.SelectedItem as PERSON;

if(selectedPerson != null)
{
textBoxAge.Text = selectedPerson.Age;
}
}

Mark R Dawson



Bruce Wood said:
First of all, you have a flaw in your user interface design. If you
have one combo box for last name and another for first name, what's to
stop your user from choosing invalid combinations of first and last
name that don't correspond to any Person on your list?

Even if you did prevent that (and it would be tricky), if you have two
people with the same last name then you're going to display that last
name twice in your Lastcombobox list. Ugly.

In order to fix up that situation, I suggest that you add some methods
to your Person class:

Change Person to implement IComparable:

public class Person : IComparable

this will force you to implement the Compare method:

public int CompareTo(object obj)
{
return CompareTo(obj as Person);
}

public int CompareTo(Person otherPerson)
{
int result;
if (otherPerson == null)
{
// Everything is greater than null
result = 1;
}
else
{
result = this.m_last.CompareTo(otherPerson.m_last);
if (result == 0)
{
result = this.m_first.CompareTo(otherPerson.m_first);
}
if (result == 0)
{
result = this.age.CompareTo(otherPerson.age);
}
return result;
}
}

Now, you can sort your array of Persons by last name then first name:

CollectionList.Sort();

Once you've done that, I would put all of the "Person"s in one combo
box, using a last name / first name display. I would create a new
property in Person:

public string ComboBoxDisplay
{
get { return this.m_last + ", " + this.m_first; }
}

then add that string to the combo box for each Person:

foreach (Person per in CollectionList)
{
this.PersoncomboBox.Items.Add(per.ComboBoxDisplay);
}

Now you have to hook up an event handler to your combo box, and write a
method to say what will happen whenever the user changes the selected
index in the combo box:

this.PersoncomboBox.SelectedIndexChanged += new
System.EventHandler(PersoncomboBox_SelectedIndexChanged);

....

private void PersoncomboBox_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
// Unfortunately, ComboBox doesn't have a Tag attribute, so you
have
// to find the chosen Person by position
int selected = this.PersoncomboBox.SelectedIndex;
if (selected < 0)
{
this.txtAge.Text = "";
}
else
{
Person chosenPerson = (Person)this.CollectionList[selected];
this.txtAge.Text = chosenPerson.PersonAge;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top