ListBox
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
| Exam 70-526 Preparation Guide: Provide a list of options on a Windows Form by using a ListBox control, a ComboBox control, or a CheckedListBox control |
Contents |
The ListBox control is used to provide a list of items from which the user may select one or more items. If a list box contains more items than can be displayed at one time, a vertical scroll bar is automatically added.
Inheritance hierarchy
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ListControl
System.Windows.Forms.ListBox
Useful properties
-
ColumnWidth- Defines the width of columns in a multicolumnListBox.
-
-
IntegralHeight- Indicates whether the control should resize to avoid showing partial items. (You may want to set this property to false, as when set to true, theListBoxlooks really jerky if it is docked within a container and the container is resized.)
-
-
MultiColumn- Indicates whether theListBoxsupports multiple columns.
-
-
SelectedIndices- Gets a collection that contains the zero-based indexes of all currently selected items in theListBox.
-
-
SelectedItems- Gets a collection containing the currently selected items in theListBox.
-
-
SelectionMode- Defines the method in which items are selected in theListBox. Takes a value from theSelectionModeenumeration:
-
-
MultiExtended- Multiple items can be selected, and the user can use the SHIFT, CTRL, and arrow keys to make selections. -
MultiSimple- Multiple items can be selected. -
None- No items can be selected. -
One- Only one item can be selected.
-
Useful methods
-
ClearSelected- Unselects all items in theListBox.
-
-
SetSelected- Selects or clears the selection for the specified item in aListBox.
-
Displaying Items
When strings are added to the Items collection, a scroll bar is automatically added to the right hand side of the control.
The size and position of the bar within the scroll bar gives a good indication of how big the list is.
Multi-column ListBox
Often when a list becomes too long, it is beneficial to show multiple columns. This is achieved by setting the MultiColumn property to true.
listBox1.MultiColumn = true;
The width of the columns can be changed by setting the ColumnWidth property to a width in pixels.
listBox1.ColumnWidth = 60;
Selecting Items manually
An item can be selected through code, by using the SelectedItem property.
listBox1.SelectedItem = "Nickelback";
More than one item can be selected by setting the SelectedItem property multiple times.
listBox1.SelectedItem = "Nickelback"; listBox1.SelectedItem = "Bee Gees";
This only applies if the SelectionMode property is set to either MultiExtended or MultiSimple.
Items can also be selected through the SelectedIndex property in the same manner.
Note:
|
MSDN references
|




