Name

ListBox

Synopsis

This Control wraps the Win32 listbox common control, allowing you to display and select one or more items from a list. It derives from the ListControl abstract base class, in common with the ComboBox.

You can add or remove objects in the list by using the Items collection. This could simply be a list of strings, but could equally be any object that can be converted to a string (which, in practice, means pretty much anything through the ToString() member).

Alternatively, you can use data binding to bind to a data source. You can independently set a DisplayMember and a ValueMember.

In either case, the selected item can be retrieved through the Text property (which returns the DisplayMember) and SelectedValue (which returns the ValueMember), or you can retrieve its index with the SelectedIndex property.

The SelectionMode allows you to specify the various kinds of single- and multiple-selection modes available. If multiple selection is enabled, you can use the SelectedIndices and SelectedItems collections to enumerate the entire selection. You can select or deselect individual items using the SetSelected() method.

If you need to locate a particular item in the list, you can use the FindString() and FindStringExact() methods. FindString() identifies the first item in the list that starts with the string specified, and is therefore extremely useful for those match-as-you-type controls.

If the items added to the control are wider than the control itself, you can enable a ...

Get .NET Windows Forms in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.