ItemData Property

Purpose

Returns or sets a specific number for each item in a ComboBox or ListBox control.

Syntax

object.ItemData(index) [= number]

number:iexp

Description

The ItemData property is an array of long integer values with the same number of items as a control's List property. You can use the numbers associated with each item to identify the items. For example, you can use an employee's identification number to identify each employee name in a ListBox control. When you fill the ListBox, also fill the corresponding elements in the ItemData array with the employee numbers.

The ItemData property is often used as an index for an array of data structures associated with items in a ListBox control.

Example

Global a$, n As Int32

Ocx ListBox lb = "", 10, 10, 100, 200

For n = 1 To 26 : a$ = "Letter " & (n < 10 ? " " : "") & Trim(n) : lb.AddItem a$, n + 64 : Next n

Do : Sleep : Until Me Is Nothing

 

Sub lb_Click

If lb.ListIndex <> -1

Message lb.List(lb.ListIndex) & " is " & Chr(lb.ItemData(lb.ListIndex))

EndIf

EndSub

Remarks

When you insert an item into a list with the AddItem method, an item is automatically inserted in the ItemData array as well. However, the value isn't reinitialized to zero; it retains the value that was in that position before you added the item to the list. When you use the ItemData property, be sure to set its value when adding new items to a list.

See Also

ListBox, ComboBox, Item, AddItem

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