Tip 2 Simple subs for moving items in a ListBox

s1ckOh

Well-known member
Joined
Aug 1, 2011
Messages
68
Location
San Diego, Ca
Programming Experience
Beginner
If there is an easier way, please share.

VB.NET:
Sub MoveItemUp()
        intStartIndex = ListBox1.SelectedIndex
        intFinishIndex = intStartIndex - 1

        Try
            ListBox1.Items.Insert(intFinishIndex, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(intStartIndex + 1)
            ListBox1.SelectedIndex = intFinishIndex
        Catch ex As Exception
            MsgBox("You have reached the start of the list")
        End Try
    End Sub

    Sub MoveItemDown()
        intStartIndex = ListBox1.SelectedIndex
        intFinishIndex = intStartIndex + 2

        Try
            ListBox1.Items.Insert(intFinishIndex, ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(intStartIndex)
            ListBox1.SelectedIndex = intStartIndex + 1
        Catch ex As Exception
            MsgBox("You have reached the end of the list")
        End Try
    End Sub
 
Top Bottom