ListBoxObject.Itemdata (RowIndex)

Dani AI

Generated

Short primer that fills the gaps in the thread (answering and responding to ):

ListBox.ItemData is a per-row numeric slot you can use for a hidden ID (database key, array index, enum value). It is not the display text — that comes from the List/Column property. Both the visible list and the ItemData use zero-based row indexes (the first row is index 0; ListIndex is -1 when nothing is selected). (learn.microsoft.com)

Practical pattern (safe with Sorted = True): add the display string, then record the ID at the index the control actually inserted — that index is available as NewIndex. Example usage in VB6:

' populate and attach a numeric ID to each item
lstProducts.AddItem Products(i).Name
lstProducts.ItemData(lstProducts.NewIndex) = Products(i).ID

Use NewIndex instead of ListCount-1 if the list is sorted. (faculty.elgin.edu)

Reading the ID back (single or multi-select): for single-selection read ItemData(ListIndex) after checking ListIndex <> -1. For multi-select iterate the ItemsSelected (or test Selected(i) in a loop) and call ItemData for each selected index. When you need richer data, keep a parallel array/UDT and store that array index in ItemData (common VB6 pattern). (learn.microsoft.com)

Notes and cautions: ItemData is intended for 32‑bit integer values (use Long for IDs). It’s not for storing strings/objects directly — use ItemData as an index into your own storage when more info is needed. SendKeys is unrelated to ListBox.ItemData and should be avoided for this purpose; inspect the control at runtime (Immediate window) and read the VB6 help (F1) as suggested for hands‑on learning. (xxxmen.github.io)

Recommended Answers

All 3 Replies

Use the additem method put a few items into a listbox. Press CTRL+BREAK and use the immediate window to find your answers.

?List1.List(1)
?List1.ItemData(1)
List1.Selected(1) = True
?List1.ListIndex

Now, you will notice that list and itemdata return different string because one is zero based and the other is one based.


Good Luck

Hi friend i still don't get what you mean about Itemdata and Method SendKeys could you please give me some things?

And what does sendkeys have to do with explanations of a listbox's properties/methods/events?

Add list box to form, make sure it is selected, press F1, and read all about it.


Good Luck

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.