views:

68

answers:

2

Hi,

i have a array of objects, for example people and info about them. how could i determine who was selected in the listbox where only their first and last name is shown? Is it even possible somehow to link a item in the listbox with a item in the array? Obviously i can't rely on SelectedIndex because when the names in the listbox get filtered it just doesn't work anymore.

In my application i have a listbox where are the names of persons and when i click on one person in the listbox i want to see their detais (address/contacs/misc). And the problem is when two persons share the same name.

+1  A: 

You could use the ListBox.SelectedItem like this... If you wanted you could create a new property to concatenate the FirstName and Surname and use it as your DisplayMember

public class Person
{
    public string FirstName { get; set; }
    public string Surname { get; set; }
}

var people = new[]
{
    new Person{FirstName = "Peter", Surname = "Pan"}, 
    new Person{FirstName = "Simon", Surname = "Cowell"}
};

var listbox = new ListBox
{
  DisplayMember = "FirstName",
  ValueMember = "FirstName",

  DataSource = people
};

var person = listbox.SelectedItem as Person;
Rohan West
this seems like it could work... i will check out in the morning.
Gabriel
Yes thank you this works... And do you have any idea how could i filter those persons? like show only persons which first name starts on "Pe".
Gabriel
i got around the search function :)
Gabriel
A: 

I would suggest adding a unique key to your list of objects. Then you would be able to use the unique key as the value for the listitems, which you could also get when an item is selected, and which you could use to identify the item in your list of objects.

Jagd