Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.
These two properties are arrays that give you access to the data in a list or combo box. These are very, very cool properties. They not only let you look up the information, but also let you change it when appropriate.
oObject.List( nIndex [, nColumn ] ) = cValue
cValue = oObject.List( nIndex [, nColumn ] )
oObject.ListItem( nItemId [, nColumn ] ) = cValue
cValue = oObject.ListItem( nItemId [, nColumn ] )
To paraphrase George Carlin, “Why are there two?” Like everything else to do with list boxes and combo boxes, it’s because there are two ways to enumerate the items there. The Index approach is based on the current position of items in the list. An item’s Index can change for all kinds of reasons. List contains the items in Index order.
An item’s ItemId, however, is fixed. Once it’s assigned, it never changes. The item’s contents might change (see AddListItem), but the ItemId stays the same. ListItem contains the items in order by ItemId.
To find the currently selected item, you use List[ ListIndex ] or ListItem[ ListItemId ]. Of course, you have to prefix each of them with all the right OOP stuff, so it’s more likely to look like:
This.List[ This.ListIndex ]
or even
ThisForm.lstMyList.ListItem[ ThisForm.lstMyList.ListItemId ]
On to what we think is pretty cool stuff. See that nColumn parameter? It works not only in a multi-column list, but any time the RowSource for the list has multiple columns. If your RowSourceType is 2 (Alias), you can access every column of the table or view the list is based on by specifying an appropriate value for nColumn. Same thing for a SQL statement (RowSourceType=3) or a Query (RowSourceType=4) or an Array (RowSourceType=5). |
What about changing items using these properties? You can do this only for lists or combos with RowSourceType set to 0 or 1. In other words, changes work only when the list of items really belongs to the control, not when it’s based on some outside data. That’s okay with us—in the other cases, we can just change the underlying data (and then call Requery to update the list).
You can disable an item in the list by preceding its text with a backslash (“\”). This technique works with RowSourceType = 0, 1 or 5. If an item needs to begin with a backslash and shouldn’t be disabled, use two backslashes.
Amazingly, you can even add items by simply assigning a value for an item that didn’t exist before. Given how hard it is to figure out AddItem and AddListItem, directly manipulating List or ListItem may be a viable alternative.
* Set a variable to the currently selected item.
cCurValue = This.List[ This.ListIndex ]
AddItem, AddListItem, ComboBox, ItemData, ItemIdData, ListBox, ListCount, ListIndex, ListItemId, Requery