none
How to move ListView Items UP / DOWN RRS feed

  • Question

  • I am not sure of how to move ListViewItems up and down, if anyone knows of a simple and quick way to do so, please reply...
    Saturday, July 29, 2017 11:20 PM

Answers

  • I did some testing and finally found a solution:

        Sub MoveListViewItemUp(ByVal _ListView As ListView)
            If Not _ListView.SelectedItems(0).Index = 0 Then
                Dim toMove As ListViewItem
                Dim oldIndex As Integer
    
                oldIndex = _ListView.SelectedItems(0).Index
                toMove = _ListView.SelectedItems(0)
    
                _ListView.Items.Remove(toMove)
                _ListView.Items.Insert(oldIndex - 1, toMove)
            End If
        End Sub
        Sub MoveListViewItemDown(ByVal _ListView As ListView)
            If Not _ListView.SelectedItems(0).Index + 1 = _ListView.Items.Count Then
                Dim toMove As ListViewItem
                Dim oldIndex As Integer
    
                oldIndex = _ListView.SelectedItems(0).Index
                toMove = _ListView.SelectedItems(0)
    
                _ListView.Items.Remove(toMove)
                _ListView.Items.Insert(oldIndex + 1, toMove)
            End If
        End Sub

    Is this the best method?

    Saturday, July 29, 2017 11:21 PM

All replies

  • I did some testing and finally found a solution:

        Sub MoveListViewItemUp(ByVal _ListView As ListView)
            If Not _ListView.SelectedItems(0).Index = 0 Then
                Dim toMove As ListViewItem
                Dim oldIndex As Integer
    
                oldIndex = _ListView.SelectedItems(0).Index
                toMove = _ListView.SelectedItems(0)
    
                _ListView.Items.Remove(toMove)
                _ListView.Items.Insert(oldIndex - 1, toMove)
            End If
        End Sub
        Sub MoveListViewItemDown(ByVal _ListView As ListView)
            If Not _ListView.SelectedItems(0).Index + 1 = _ListView.Items.Count Then
                Dim toMove As ListViewItem
                Dim oldIndex As Integer
    
                oldIndex = _ListView.SelectedItems(0).Index
                toMove = _ListView.SelectedItems(0)
    
                _ListView.Items.Remove(toMove)
                _ListView.Items.Insert(oldIndex + 1, toMove)
            End If
        End Sub

    Is this the best method?

    Saturday, July 29, 2017 11:21 PM
  • I did some testing and finally found a solution:

    "Finally" ... in two minutes?

    Try moving the top item up or the bottom item down. IndexOutOfRangeException maybe?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Saturday, July 29, 2017 11:28 PM