locked
Move items up and down in Listbox RRS feed

  • Question

  • Hi,

    I'm trying to move items in a Listbox up and down and I can't seem to find the answer.

    I'm finding a lot of examples but none from VB.Net 2005 express. Does anyone have examples or references on how to do this?

     

    Thanks

    Friday, July 20, 2007 1:54 PM

Answers

  • I am using this sub in my code to do the same - seems to work OK

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveUp.Click

    If ListBox1.SelectedItems.Count > 1 Then

    MsgBox("You can only move a single item")

    Exit Sub

    Else

    If ListBox1.SelectedItems.Count < 1 Then

    MsgBox("You need to select an item before you can move it.")

    Exit Sub

    End If

    End If

    Dim j As Integer = ListBox1.SelectedIndex

    Dim s As String = ListBox1.SelectedItem

    If j < 1 Then Exit Sub

    ListBox1.Items.RemoveAt(j)

    ListBox1.Items.Insert(j - 1, s)

    ListBox1.SetSelected(j - 1, True)

    End Sub

    Friday, July 20, 2007 3:41 PM

All replies

  • I am using this sub in my code to do the same - seems to work OK

     

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MoveUp.Click

    If ListBox1.SelectedItems.Count > 1 Then

    MsgBox("You can only move a single item")

    Exit Sub

    Else

    If ListBox1.SelectedItems.Count < 1 Then

    MsgBox("You need to select an item before you can move it.")

    Exit Sub

    End If

    End If

    Dim j As Integer = ListBox1.SelectedIndex

    Dim s As String = ListBox1.SelectedItem

    If j < 1 Then Exit Sub

    ListBox1.Items.RemoveAt(j)

    ListBox1.Items.Insert(j - 1, s)

    ListBox1.SetSelected(j - 1, True)

    End Sub

    Friday, July 20, 2007 3:41 PM
  • Thanks leshay,

    Works great.

    Friday, July 20, 2007 5:54 PM
  • I have a similar issue, only I am trying to figure out how to code a button to move the first item to the bottom of the list so it rotates. The purpose for this is because Im building a program to hold a list of singers in a karaoke room.  
    Thursday, July 12, 2012 9:00 PM
  • If you have a similar issue, I suggest not to reply to a thred from 2007 that is marked as answered, but open a new one instead.

    Armin

    Friday, July 13, 2012 2:28 AM