Re: Arraylist question
- From: "Mark R. Dawson" <MarkRDawson@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 24 Aug 2005 19:54:03 -0700
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" wrote:
> 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;
> }
> }
>
>
.
- Follow-Ups:
- Re: Arraylist question
- From: ttan
- Re: Arraylist question
- References:
- Arraylist question
- From: ttan
- Re: Arraylist question
- From: Bruce Wood
- Arraylist question
- Prev by Date: RE: Reading XML Schema
- Next by Date: Re: Semaphores and .NET
- Previous by thread: Re: Arraylist question
- Next by thread: Re: Arraylist question
- Index(es):
Relevant Pages
|