O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Store Objects in a List

Problem

You want to store custom objects in a list, and customize their display.

Solution

You can add custom objects directly to the ListBox.Items collection. However, you must override the ToString method in your custom class to set the text that will appear in the ListBox, or set the ListBox.DisplayMember property.

Discussion

The .NET list controls (the ListBox, ComboBox, and CheckedListBox) can hold any type of object, whether it’s an ordinary string or a custom class or structure that contains several pieces of information. However, if you attempt to store custom classes in a list box, you might discover that the text does not appear correctly. Instead, the fully qualified class name will be shown for each item in the list. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required