Logo

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.

Selected, SelectedId

These properties tell you which items in a list or combo are highlighted.

Usage

lIsHighlighted = oObject.Selected( nIndex )
oObject.Selected( nIndex ) = lIsHighlighted
lIsHighlighted = oObject.SelectedId( nItemId )
oObject.SelectedId( nItemId ) = lIsHighlighted

These two array properties let you figure out which items in a list or combo are highlighted and let you highlight items. Like the other properties related to lists, there are two of these because one gives you access via the item’s Index and the other lets you use the item’s unique ItemId. (See AddItem for more on this topic.)

In VFP 3, SelectedId doesn't work at all. Ever. No matter what's highlighted or which value you check, SelectedId is always .F. Unless you explicitly set it to .T. But that doesn't do much good, because the item still doesn't get highlighted. In addition, neither of these properties works for combos in VFP 3. Both bugs are fixed in VFP 5.

Selected is most useful when MultiSelect is .T. Then it lets you find all the highlighted items. With MultiSelect set to .F., you can find a single highlighted item with ListIndex or ListItemId—no need to check Selected or SelectedId. For the same reason, these properties aren’t terribly useful with combos.

Example

* Set up a multiselect listbox with Employees
* Preselect all those located in the USA
* Set these properties in the Prop Sheet
MultiSelect = .T.
RowSourceType = 2   && Alias
RowSource = "Employee"
ColumnCount = 3
ColumnWidths = 80,40,70

* In the Init for the list:
LOCAL nCount

SELECT Employee
nCount = 1
SCAN
   IF Country = "USA"
      This.Selected[nCount] = .T.
   ENDIF
   nCount = nCount + 1
ENDSCAN

Be aware that, in VFP 3, no more than 60 items can be selected at the same time. See MultiSelect for more on this bug.

See Also

AddItem, ComboBox, ListBox, ListIndex, ListItemId, MultiSelect