Tuesday, June 26, 2007

Moving Listview Items Up or Down

Here is a good example in VB.NET and C# for moving an item (row) in a listview up or down one position. This function simplifies implementing the Move Up and Move Down button on a form:

http://www.knowdotnet.com/articles/listviewmoveitem.html

This example suffers from one problem that I found. The .tag objects are not moved with the text. I had to add an object variable and then add the following code:

Move Up Section:

tagCache = .Items(selIdx - 1).Tag
.Items(selIdx - 1).Tag = .Items(selIdx).Tag
.Items(selIdx).Tag = tagCache


Move Down Section:

tagCache = .Items(selIdx + 1).Tag
.Items(selIdx + 1).Tag = .Items(selIdx).Tag
.Items(selIdx).Tag = tagCache

6 comments:

Anonymous said...

it also doesn't take into account multiselect listviews, you'll then have to loop through the indexes of listView.SelectedItems.

Anonymous said...

Try this:
Dim NewItem As ListViewItem = Me.lvPositions.SelectedItems(0).Clone
Me.lvPositions.Items.Insert(Me.lvPositions.SelectedIndices(0) - 1, NewItem)
Me.lvPositions.SelectedItems(0).Remove()
NewItem.Selected = True

Richard

Anonymous said...

wow thanks later anon!

Anonymous said...

Hi,
it isnot woking within the groups.

Unknown said...

Private Sub MoveListViewItem(ByVal lvItem As ListViewItem, ByVal Direction As enuDirection)
Dim strTrace As String = "General Fault."
Dim strRoutine As String = "MoveListViewItem"
Try
If Direction = enuDirection.Up Then
strTrace = "Can't move the first item up."
If lvItem.Index = 0 Then
Exit Sub
End If

strTrace = "Move the item up."
Dim NewItem As ListViewItem = lvItem.Clone
Me.lv_ActionPlan.Items.Insert(lvItem.Index - 1, NewItem)
lvItem.Remove()

strTrace = "Select the item in its new location."
NewItem.Selected = True
Else
strTrace = "Can't move the last item down."
If lvItem.Index = Me.lv_ActionPlan.Items.Count - 1 Then
Exit Sub
End If

strTrace = "Move the item down."
Dim NewItem As ListViewItem = lvItem.Clone
Dim newIndex As Integer = lvItem.Index + 1
lvItem.Remove()
If newIndex > Me.lv_ActionPlan.Items.Count - 1 Then
Me.lv_ActionPlan.Items.Add(NewItem)
Else
Me.lv_ActionPlan.Items.Insert(newIndex, NewItem)
End If

strTrace = "Select the item in its new location."
NewItem.Selected = True
End If

Catch ex As Exception
TraceLogger(strTrace, ex, strRoutine)
ErrorLogger(ex.Message & " " & strTrace, Err.Number, strRoutine)
End Try
End Sub

Anonymous said...

you can use this:

Private Sub lvSearch_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvSearch.KeyDown
If e.KeyCode = Keys.Down Then

If lvSearch.SelectedItems(0).Index < (lvSearch.Items.Count - 1) Then

txtName.Text = lvSearch.Items(lvSearch.SelectedItems(0).Index + 1).SubItems(1).Text

End If
End If
End Sub

Private Sub lvSearch_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lvSearch.KeyUp
If e.KeyCode = Keys.Up Then
If lvSearch.SelectedItems(0).Index >= 0 Then

txtName.Text = lvSearch.Items(lvSearch.SelectedItems(0).Index).SubItems(1).Text

End If
End If
End Sub