List, ListCount, ListIndex Properties

Purpose

List returns or sets the items contained in a ComboBox or ListBox object's list portion. ListCount returns the number of items in the list portion of a control. ListIndex returns or sets the index of the currently selected item in the control.

Syntax

object.List(index) [= string]

object.ListIndex [= index%]

object.ListCount

object:ComboBox or ListBox Ocx

Description

The list is a string array in which each element is a list item. List(0) returns the first entry. The List property works in conjunction with the ListCount and ListIndex properties. Enumerating a list from 0 to ListCount -1 returns all items in the list.

ListIndex returns or sets the currently selected list item, it takes an index from 0 to ListCount - 1. When no item is selected, ListIndex returns -1.

Example

Debug.Show

~SetWindowPos(Debug.hWnd, 0, 205, 10, 300, 500, 0)

OpenW 1, 10, 10, 185, 350

Local n As Int32

Ocx ListBox lb = "", 10, 10, 150, 300 : .Sorted = False : .IntegralHeight = True

For n = 1 To 30 : lb.AddItem "List Item" & n : Next n

//Enumerate the list in Debug window

For n = 1 To lb.ListCount : Debug "List(" & Trim(n - 1) & ") = " & lb.List(n - 1) : Next n

Debug.Print

Do : Sleep  : Until Me Is Nothing

Debug.Hide

 

Sub lb_Click

If lb.ListIndex <> -1 // An item has been selected

Debug "Selected Item: "; lb.ListIndex; " - ";#34;lb.List(lb.ListIndex);#34

EndIf

EndSub

Remarks

To specify items you want to display in a ComboBox or ListBox control, use the AddItem method. To remove items, use the RemoveItem method. To keep items in alphabetic order, set the control's Sorted property to True before adding items to the list.

See Also

ListBox, ComboBox

{Created by Sjouke Hamstra; Last updated: 12/10/2014 by James Gaite}