Hi try this, place two buttons and a listbox on a form, button1.text = UP,
button2.text = DOWN, the listbox should have selectionmode set to one. Now
you can try this code.
Hth Greetz Peter
Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
ListBox1.Items.Add("First")
ListBox1.Items.Add("Second")
ListBox1.Items.Add("Third")
ListBox1.Items.Add("Fourth")
ListBox1.Items.Add("Fifth")
ListBox1.Items.Add("Sixth")
ListBox1.Items.Add("Seventh")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If Not ListBox1.SelectedItem Is Nothing Then
moveup()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If Not ListBox1.SelectedItem Is Nothing Then
movedown()
End If
End Sub
Private Sub moveup()
Dim tmpItem1, tmpItem2 As Object
Dim intIndex As Integer
If ListBox1.SelectedIndex > 0 Then
intIndex = ListBox1.SelectedIndex
tmpItem1 = ListBox1.Items.Item(intIndex)
tmpItem2 = ListBox1.Items.Item(intIndex - 1)
ListBox1.Items.RemoveAt(intIndex)
ListBox1.Items.RemoveAt(intIndex - 1)
ListBox1.Items.Insert(intIndex - 1, tmpItem2)
ListBox1.Items.Insert(intIndex - 1, tmpItem1)
ListBox1.SelectedItem = ListBox1.Items.Item(intIndex - 1)
End If
End Sub
Private Sub movedown()
Dim tmpItem1, tmpItem2 As Object
Dim intIndex As Integer
If ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
intIndex = ListBox1.SelectedIndex
tmpItem1 = ListBox1.Items.Item(intIndex)
tmpItem2 = ListBox1.Items.Item(intIndex + 1)
ListBox1.Items.RemoveAt(intIndex)
ListBox1.Items.RemoveAt(intIndex)
ListBox1.Items.Insert(intIndex, tmpItem2)
ListBox1.Items.Insert(intIndex + 1, tmpItem1)
ListBox1.SelectedItem = ListBox1.Items.Item(intIndex + 1)
End If
End Sub