See Also: ListBox Members
The System.Windows.Forms.ListBox control enables you to display a list of items to the user that the user can select by clicking. A System.Windows.Forms.ListBox control can provide single or multiple selections using the ListBox.SelectionMode property. The System.Windows.Forms.ListBox also provides the ListBox.MultiColumn property to enable the display of items in columns instead of a straight vertical list of items. With this, the control can display more visible items and the user no longer needs to scroll to an item.
Typically, Windows handles the task of drawing the items to display in the System.Windows.Forms.ListBox. You can use the ListBox.DrawMode property, and handle the ListBox.MeasureItem and ListBox.DrawItem events so you can override the automatic drawing that Windows provides and draw the items yourself. You can use owner-drawn System.Windows.Forms.ListBox controls to display variable-height items, images, or a different color or font for the text of each item in the list. The ListBox.HorizontalExtent property, ListBox.GetItemHeight(int), and ListBox.GetItemRectangle(int) also help you draw your own items.
In addition to display and selection functionality, the System.Windows.Forms.ListBox also provides features that enable you to efficiently add items to the System.Windows.Forms.ListBox and to find text within the items of the list. The ListBox.BeginUpdate and ListBox.EndUpdate methods enable you to add a large number of items to the System.Windows.Forms.ListBox without the control being repainted each time an item is added to the list. The ListBox.FindString(string) and ListBox.FindStringExact(string) methods enable you to search for an item in the list that contains a specific search string.
The ListBox.Items, ListBox.SelectedItems, and ListBox.SelectedIndices properties provide access to the three collections that are used by the System.Windows.Forms.ListBox. The following table outlines the three collections used by the System.Windows.Forms.ListBox and their use within the control.
System.Windows.Forms.ListBox.ObjectCollection |
Contains all items contained in the System.Windows.Forms.ListBox control. |
System.Windows.Forms.ListBox.SelectedObjectCollection |
Contains a collection of the selected items which is a subset of the items contained in the System.Windows.Forms.ListBox control. |
System.Windows.Forms.ListBox.SelectedIndexCollection |
Contains a collection of the selected indexes, which is a subset of the indexes of the System.Windows.Forms.ListBox.ObjectCollection. These indexes specify items that are selected. |
The following three examples show the three indexed collections that the System.Windows.Forms.ListBox class supports.
The following table shows an example of how the System.Windows.Forms.ListBox.ObjectCollection stores the items of the System.Windows.Forms.ListBox as well as their selection state within an example System.Windows.Forms.ListBox.
0 |
object1 |
Unselected |
1 |
object2 |
Selected |
2 |
object3 |
Unselected |
3 |
object4 |
Selected |
4 |
object5 |
Selected |
Based on the System.Windows.Forms.ListBox.ObjectCollection shown in the previous table, this table shows how the System.Windows.Forms.ListBox.SelectedObjectCollection would appear.
0 |
object2 |
1 |
object4 |
2 |
object5 |
Based on the System.Windows.Forms.ListBox.ObjectCollection shown in the previous table, this table shows how the System.Windows.Forms.ListBox.SelectedIndexCollection would appear.
0 |
1 |
1 |
3 |
2 |
4 |
The System.Windows.Forms.ListBox.ObjectCollection.Add(object) method of the System.Windows.Forms.ListBox.ObjectCollection class enables you to add items to the System.Windows.Forms.ListBox. The System.Windows.Forms.ListBox.ObjectCollection.Add(object) method can accept any object when adding a member to the System.Windows.Forms.ListBox. When an object is being added to the System.Windows.Forms.ListBox, the control uses the text defined in the object.ToString method of the object unless a member name within the object is specified in the ListControl.DisplayMember property. In addition to adding items using the System.Windows.Forms.ListBox.ObjectCollection.Add(object) method of the System.Windows.Forms.ListBox.ObjectCollection class you can also add items using the ListControl.DataSource property of the System.Windows.Forms.ListControl class.
If you have a System.Windows.Forms.ListBox, System.Windows.Forms.ComboBox, or System.Windows.Forms.CheckedListBox on a base Windows form and want to modify the string collections of those controls in a derived Windows form, the string collections of those controls in the base Windows form must be empty. If the string collections are not empty, they become read-only when you derive another Windows form.