By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,396 Members | 2,501 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,396 IT Pros & Developers. It's quick & easy.

How to make first selected item in a list box visible

P: n/a
I've tried googling for the answer to this problem, without any luck.
I'm sure the truth must be out there somewhere!

I have a multiselect listbox populated with many items (set by the
RowSource property). In my code I cause a number of these to be
selected - but if the first selected item is not within the currently
visible range then there is no change to the display. This gives the
impression that nothing has happened.

I think that for a single select box the display moves automatically
to include the selected item. How can I arrange for something
comparable to occur with an extended multiselect listbox?

Thanks in advance

Peter

Mar 22 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
pe**********@talk21.com wrote:
I've tried googling for the answer to this problem, without any luck.
I'm sure the truth must be out there somewhere!

I have a multiselect listbox populated with many items (set by the
RowSource property). In my code I cause a number of these to be
selected - but if the first selected item is not within the currently
visible range then there is no change to the display. This gives the
impression that nothing has happened.

I think that for a single select box the display moves automatically
to include the selected item. How can I arrange for something
comparable to occur with an extended multiselect listbox?

Thanks in advance

Peter
Take off wordwrap if link wraps. I think it will point you to a solution.
http://groups.google.com/group/micro...47aabc70050e8f
Mar 22 '07 #2

P: n/a
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

Mar 23 '07 #3

P: n/a
I haven't tried this but what happens if you go through the list
backwards so the last item selected is at the top?

Mar 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.