Thank you for those hints. Looking through the code, I found that
the .ListIndex method does seem to move the display to accommodate the
selection, while the .Selected(i)=True method does not. The trouble
is that the .ListIndex method also overwrites previous selections,
which is a problem on a multiselect list.
My solution was to use a flag initially set to false. Then if the
flag is false, set the selection using .ListIndex and change the flag
to true, else use .Selected(i)=True. I also iintroduced an initial
dummy selection of the last index item which moved the display to the
end of the list. When this was overwritten by the first selection,
the display moved up such that the first selected item was at the top
of the list.
My code is given below. This code sits within a larger loop in which
successive values of 'a' are read from a file, and if they match an
item in the listbox lstOffenceCodes then that item is selected. The
flag OffenceCodeFlag is initially cleared.
lstOffenceCodes.SetFocus
For i = 0 To lstOffenceCodes.ListCount - 1
If lstOffenceCodes.ItemData(i) = a Then
If Not OffenceCodeFlag Then
' If first selection from listbox then
' Make a dummy selection of the last
listbox item
' This will cause the display to move to
the end of the list
lstOffenceCodes.ListIndex =
lstOffenceCodes.ListCount - 1
' Now make the actual selection. This will
overwrite the
' dummy selection, and ensure the selected
item appears
' towards the top of the list box
lstOffenceCodes.ListIndex = i
' Set the flag so future selections do not
disturb the display
OffenceCodeFlag = True
Else
' If not the first selection from listbox
then
' Make selection via the .Selected property
' This does not cause the display to change
lstOffenceCodes.Selected(i) = True
End If
End If
Next i