Click to See Complete Forum and Search --> : Swap items in ListBox


Jdev
07-30-2004, 11:52 AM
Hello, I have a ListBox (ListBox1) and two buttons. (cmdUp and cmdDown) How can I swap the items in my ListBox when an item is selected and the user clicks the up or down button?

Thanks.

jaideepvr
07-30-2004, 12:04 PM
Try using Listbox.Items.Insert and Listbox.Items.RemoveAt Methods.
- Jaideep

Jdev
07-30-2004, 01:25 PM
Thanks. Here is what I ended up doing for the "cmdUp" click event.

If Me.ListBox1.SelectedIndex <> 0 Then
Dim sSelectedItem As String = Me.ListBox1.Items(Me.ListBox1.SelectedIndex)
Dim sPriorItem As String = Me.ListBox1.Items(Me.ListBox1.SelectedIndex - 1)

Me.ListBox1.Items(Me.ListBox1.SelectedIndex) = sPriorItem
Me.ListBox1.Items(Me.ListBox1.SelectedIndex - 1) = sSelectedItem

Me.ListBox1.SetSelected(Me.ListBox1.SelectedIndex - 1, True)
Else
Exit Sub
End If