ListBox


The ListBox control displays a list of items that the user can select. The following table describes some of the control’s most useful properties.

Open table as spreadsheet

Property

Purpose

SelectionMode

Determines how the user can select text. See the following text for details.

MultiColumn

If this is True, the control does not display a vertical scroll bar. Instead, if there are too many items to fit, the control displays them in multiple columns. If the columns will not all fit, the control displays a horizontal scroll bar to let you see them all. The control’s ColumnWidth property determines the width of the columns.

IntegralHeight

If this is True, the control will not display a partial item. For example, if the control is tall enough to display 10.7 items, it will shrink slightly so it can only display 10 items.

ScrollAlwaysVisible

If this is True, the control displays its vertical scroll bar even if all of its items fit. This can be useful if the program will add and remove items to and from the list at runtime and you don’t want the control to change size depending on whether the items all fit.

Sorted

Determines whether the control displays its items in sorted order.

SelectedItem

Returns a reference to the first selected item or Nothing if no item is selected. This is particularly useful if the control’s SelectionMode is One.

SelectedIndex

Returns the zero-based index of the first selected item or -1 if no item is selected. This is particularly useful if the control’s SelectionMode is One.

Text

Returns the text displayed for the first currently selected item, or an empty string if no item is selected. Your code can set this property to a string to make the control select the item that displays exactly that string.

SelectedItems

A collection containing references to all the items that are currently selected

SelectedIndices

A collection containing the indexes of all the items that are currently selected.

UseTabStops

Determines whether the control recognizes tabs embedded within its items’ text. If UseTabStops is True, the control replaces tab characters with empty space. If UseTabStops is False, the control displays tab characters as thin black boxes.

The control’s SelectionMode property determines how the user can select items. This property can take the value None, One, MultiSimple, or MultiExtended.

When SelectionMode is None, the user cannot select any items. This mode can be useful for displaying a read-only list. It can be particularly handy when the list is very long and the control’s automatic scrolling is useful, allowing the user see all of the list’s items.

When SelectionMode is One, the user can select a single item. When the user clicks on an item, any previously selected item is deselected.

When SelectionMode is MultiSimple, the user can select multiple items by clicking on them one at a time. When the user clicks on an item, the other items keep their current selection status. This mode is useful when the user needs to select multiple items that are not necessarily near each other in the list. It is less useful if the user must select a large number of items. For example, clicking 100 items individually would be tedious.

When SelectionMode is MultiExtended, the user can select multiple items in several ways. The user can click and drag to manipulate several items at once. If the first item clicked is not selected, then all of the items are selected. If the first item is already selected, then all of the items are deselected. If the user holds down the Ctrl key and clicks an item, the other items’ selection status remains unchanged. If the user doesn’t hold down the Ctrl key, any other items are deselected when the user selects new items. If the user clicks on an item and then clicks on another item while holding down the Shift key, all of the items between those two are selected. If the user holds down the Ctrl key at the same time, those items are selected and the other items’ selection status remains unchanged.

The MultiExtended mode is useful when the user needs to select many items, some of which may be next to each other. This mode is quite complicated, however, so MultiSimple may be a better choice if the user doesn’t need to select ranges of items or too many items.

Use the control’s Items.Add method to add an object to the list. If the object isn’t a string, the control uses the object’s ToString method to determine the value it displays to the user.

The ListBox control’s FindString method returns the index of the first item that begins with a given string. For example, the following code selects the first item that begins with the string Code:

  lstTask.SelectedIndex = lstTask.FindString("Code") 

The FindStringExact method returns the index of the first item that matches a given string exactly.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net