Re: Arraylist question



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;
> }
> }
>
>
.



Relevant Pages

  • Re: ListItem not available in win forms
    ... You can put anything in a combobox, be it a string, int, float or a custom ... The combobox will call the ToString method of the object to find out ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Data in ComboBoxes
    ... DotNet that the item type is object and the control uses the ToString() ... method to figure out how to display it. ... public string ToString() ... > access the data of the combobox either by the items text or by its index. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Combobox Problem
    ... ist dann immer das Ergebnis der ToString() Methode des jeweiligen Objects. ... public overide string ToString() ... > Wie kann ich in einer Combobox 2 Werte mitliefern, ... > haben sollte ich die Bezeichnung darin haben. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Fill a ComboBox
    ... the combobox and uses the ToStringmethod to get the text to display. ... Simply create a class that has a String and an Integer and add a ToString() ... > I want to display some information, but when I select a item, I ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Fill a ComboBox
    ... the combobox and uses the ToStringmethod to get the text to display. ... Simply create a class that has a String and an Integer and add a ToString() ... > I want to display some information, but when I select a item, I ...
    (microsoft.public.dotnet.general)