
ListView: How to get item position
Eli,
The following code, which uses a VScrollBar as a spin control to move an
item up or down in a ListView control. (The scroll bar has min and max set
to 0 and 2, respectively, and is initialized to 1). I hope this fragment
gives you some ideas.
Richard Muller
Private Sub ctlVMove_Change()
Dim nOldValue As Integer: nOldValue = ctlVMove.Value - 1
Dim nPrevItem: nPrevItem = nCurrentItem
ctlVMove.Value = 1 ' Causes extra invocation of ctlVMove_Change with
oldval=1
If nOldValue = -1 Then ' Moving toward top of list
If nCurrentItem > 1 Then
nCurrentItem = nCurrentItem - 1
End If
Else
' Moving toward bottom of list (if nOldValue=2)
If nOldValue = 1 And nCurrentItem < lvTableNames.ListItems.Count
Then
nCurrentItem = nCurrentItem + 1
End If
End If
Dim sTemp As String
Dim liPrev As ListItem: Set liPrev = lvTableNames.ListItems(nPrevItem)
Dim liCurr As ListItem: Set liCurr =
lvTableNames.ListItems(nCurrentItem)
liPrev.Selected = False
liCurr.Selected = True
sTemp = liPrev.Text
liPrev.Text = liCurr.Text
liCurr.Text = sTemp
lvTableNames.SetFocus
End Sub