Changing how a ListBox displays an object.

  • Thread starter Thread starter Dave Rudolf
  • Start date Start date
D

Dave Rudolf

Hi all,

I have a System.Windows.Forms.ListBox object that I am putting objects into.
These objects are of custom types that I am defining. The list box displays
each object by its class name -- not exactly usable :). So what I would like
is to control the text that is displayed. Apparently, overridding the
ToString method does not work. So what is the trick?

I know that in Java, you can simply create a custom renderer that will
control how an object is displayed. Is there a similar thing in C#?

Dave.
 
quoting from MSDN
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemWindows
FormsListBoxClassTopic.htm

"When an object is being added to the ListBox, the control uses the text
defined in the ToString method of the object unless a member name within the
object is specified in the DisplayMember property. In addition to adding
items using the Add method of the ListBox.ObjectCollection class you can
also add items using the DataSource property of the ListControl class."

Rakesh Ravuri
 
Hi all,

I have a System.Windows.Forms.ListBox object that I am putting objects into.
These objects are of custom types that I am defining. The list box displays
each object by its class name -- not exactly usable :). So what I would like
is to control the text that is displayed. Apparently, overridding the
ToString method does not work. So what is the trick?

Overriding the ToString method *does* work. Double-check your code.
 
Back
Top