Select item in listbox with code

  • Thread starter Thread starter mschirle
  • Start date Start date
M

mschirle

I'm looking for the ability to delete an item from a list and after the item
is deleted and the list is requeried i would like the cursor to be located on
the item just above the item deleted. I've captured the item index id prior
to deletion I'm just not sure how to relocate the cursor to an item via code.
Thanks for your help.
 
mschirle said:
I'm looking for the ability to delete an item from a list and after the
item
is deleted and the list is requeried i would like the cursor to be located
on
the item just above the item deleted. I've captured the item index id
prior
to deletion I'm just not sure how to relocate the cursor to an item via
code.
Thanks for your help.


Something like this:

'----- start of "air code" -----
Dim lngSelectedRow As Long

With Me.lstYourListBox

' Capture selected row.
If Not IsNull(.Value) Then
lngSelectedRow = .ItemsSelected(0)
Else
lngSelectedRow = 0
End If

' ... Do something to delete this item; then ...

' Select the previous item
If lngSelectedRow > 0 Then
lngSelectedRow = lngSelectedRow - 1
End If

.Value = .ItemData(lngSelectedRow)

End With
'----- end of "air code" -----

That assumes that the list box is not in multi-select mode. The code is
untested "air code", of course, and may require some tweaking.
 
Back
Top