Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reordering Listview

Status
Not open for further replies.

MSMINNICH

Programmer
Oct 3, 2001
33
0
0
US
All-

I have a screen where I have a Listview control with multiple subitems (columns from a query). The order of the ListItems is significant. I want to give the user an option of reordering the ListItems by selecting one and using an up or down command button. I can't seem to make this work. My code is below... Thanks in advance...

Dim itmX As ListItem
Dim itmY As ListItem
Dim SelIndex As Integer

SelIndex = lstDiscTree.SelectedItem.Index
'Back up SELECTED item
Set itmX = lstDiscTree.ListItems.Item(SelIndex)
'Remove the SELECTED ITEM
lstDiscTree.ListItems.Remove (SelIndex)

Set itmY = lstDiscTree.ListItems.Add((SelIndex - 1))
Set itmY = itmX
 
I couldn't get your way to work either. But, until something better comes along, this is what I came up with.

Private Const NumOfColumns = 3 'Change this to the number of columns in your listview
Private ColText(NumOfColumns) As String

Private Sub cmdMoveUp_Click()
Dim itmX As ListItem
Dim itmY As ListItem
Dim SelIndex As Integer
Dim j As Integer

SelIndex = lstDiscTree.SelectedItem.Index

'Make sure you can move up
If SelIndex > 1 Then
'Save selected item
Set itmX = lstDiscTree.ListItems(SelIndex)

'Save selected item - 1
Set itmY = lstDiscTree.ListItems(SelIndex - 1)

'Put itmX into array
ColText(0) = itmX
For j = 1 To NumOfColumns - 1
ColText(j) = itmX.SubItems(j)
Next j

'Put itmY in itmX
itmX = itmY
For j = 1 To NumOfColumns - 1
itmX.SubItems(j) = itmY.SubItems(j)
Next j

'Put array into itmY
itmY = ColText(0)
For j = 1 To NumOfColumns - 1
itmY.SubItems(j) = ColText(j)
Next j

lstDiscTree.Refresh
lstDiscTree.ListItems(SelIndex - 1).Selected = True
lstDiscTree.SetFocus

Set itmX = Nothing
Set itmY = Nothing
End If

End Sub

Private Sub cmdMoveDown_Click()
Dim itmX As ListItem
Dim itmY As ListItem
Dim SelIndex As Integer
Dim j As Integer

SelIndex = lstDiscTree.SelectedItem.Index

'Make sure you can move down
If SelIndex < lstDiscTree.ListItems.Count Then
'Save selected item
Set itmX = lstDiscTree.ListItems(SelIndex)

'Save selected item + 1
Set itmY = lstDiscTree.ListItems(SelIndex + 1)

'Put itmX into array
ColText(0) = itmX
For j = 1 To NumOfColumns - 1
ColText(j) = itmX.SubItems(j)
Next j

'Put itmY in itmX
itmX = itmY
For j = 1 To NumOfColumns - 1
itmX.SubItems(j) = itmY.SubItems(j)
Next j

'Put array into itmY
itmY = ColText(0)
For j = 1 To NumOfColumns - 1
itmY.SubItems(j) = ColText(j)
Next j

lstDiscTree.Refresh
lstDiscTree.ListItems(SelIndex + 1).Selected = True
lstDiscTree.SetFocus

Set itmX = Nothing
Set itmY = Nothing
End If

lstDiscTree.SetFocus

End Sub

Hope this helps
 
That's pretty much how I ended up doing it. It just semed to me that I should be able to accomplish the same thing using the objects... Thanks a bunch for your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top