Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display specified text for listbox items

I have a list that contains custom objects. These objects have different properties, and I have ~100 of them. I want to create a list of them in a listbox, but the listbox displays only

  MyNamespace.MyClass
  MyNamespace.MyClass
  MyNamespace.MyClass
  MyNamespace.MyClass
  ...

Is it possible to make the listbox display a certain value for each item? Lets say my objects have an ID string value. Can I display the ID for each item without discarding my objects' other properties?

I currently fill the listbox this way:

  lbox.Items.Clear();
  lbox.Items.AddRange(list.ToArray());
like image 310
zergerge Avatar asked Dec 25 '22 06:12

zergerge


1 Answers

Set the DisplayMember to the property of your class that you'd like the user to see.

lbox.Items.Clear();
lbox.Items.AddRange(list.ToArray());

lbox.DisplayMember = "ID";  // ID is a public property in MyClass
like image 132
Grant Winney Avatar answered Jan 06 '23 01:01

Grant Winney