Move item in listbox up or down

Lanc1988

Contributor
Joined
Nov 27, 2003
Messages
508
I have a listbox in which users can add items to it.. i would like to have a button that lets them move the selected item either up or down.

I called my checkedlistbox "listboxMiscGoals"
 
Assuming you are using vb...

Code:
    Sub MoveSelectedItemUp(ByVal Box As ListBox)
        Dim Index As Integer = Box.SelectedIndex    'Index of selected item
        Dim Swap As Object = Box.SelectedItem       'Selected Item
        If Not (Swap Is Nothing) Then               'If something is selected...
            Box.Items.RemoveAt(Index)                   'Remove it
            Box.Items.Insert(Index - 1, Swap)           'Add it back in one spot up
            Box.SelectedItem = Swap                     'Keep this item selected
        End If
    End Sub

If you are c# then... this is PROBABLY WRONG but you can get the jist of it
Code:
    void MoveSelectedItemUp(ListBox Box) {
        int Index = Box.SelectedIndex;          //Selected Index
        object Swap = Box.SelectedItem;      //Selected Item
        If (Index == -1) {               //If something is selected...
            Box.Items.RemoveAt(Index);                 //Remove it
            Box.Items.Insert(Index - 1, Swap);        //Add it back in one spot up
            Box.SelectedItem = Swap;                   //Keep this item selected
        }
    }
 
im having a problem when its already at the top and they click to move it up.. i stopped the error from happening, but it is completely removing the goal.

I tried adding Try, Catch, and End Try, but i dont know what to put under Catch to make it insert the item in the first place.
 
Ideally if the selected item is already at the top you want to disable the "move up" button since it cant be moved up. If you prefer not to, then replace the line "If Not (Swap Is Nothing) Then" with "If Index > 0 Then". This will only move items with an index 1 or higher (i.e. the second or higher item on the list).
 
ok, when i replaced it with If Index > 0 Then it works fine for the move up, but what would i replace the If Not (Swap is nothing) part with for my move down button?
 
Just as the "Index > 0" condition tests if the selected item's index is greater than that of the first item, the move down function should test if the selected index is less than that of the last item. Keep in mind that you also need to either test if the index = -1 or if Swap is Nothing, either of which will indicate that nothing is selected. So... here. Have some code.

Visual Basic:
    Sub MoveSelectedItemDown(ByVal Box As ListBox)
        Dim Index As Integer = Box.SelectedIndex    'Index of selected item
        Dim Swap As Object = Box.SelectedItem       'Selected Item
        If (Index <> -1) AndAlso (Index + 1 < Box.Items.Count) Then
            Box.Items.RemoveAt(Index)                   'Remove it
            Box.Items.Insert(Index + 1, Swap)           'Add it back in one spot up
            Box.SelectedItem = Swap                     'Keep this item selected
        End If
    End Sub
 
This implementation does it all on a single for loop, it's controlled by the index parameter, it must be -1 to move the item up (the index decreases) or 1 to move the item down (the index increases). It also works on a multiple selection listbox. The loop dynamically adjusts its direction depending on the index parameter:

Code:
private void MoveListboxItem(int index, ListBox listBox)
        {
            if (listBox.SelectedIndex != -1) //is there an item selected?
            {
//if it's moving up, the loop moves from first to last, otherwise, it moves from last to first
                for (int i = (index < 0 ? 0 : listBox.Items.Count - 1); index < 0 ? i < listBox.Items.Count : i > -1; i -= index)
                {
                    if (listBox.Items[i].Selected)
                    {
                       //if it's moving up, it should not be the first item, or, if it's moving down, it should not be the last
                        if ((index < 0 && i > 0) || (index > 0 && i < listBox.Items.Count - 1))
                        {
                         //if it's moving up, the previous item should not be selected, or, if it's moving down, the following item should not be selected
                            if ((index < 0 && !listBox.Items[i - 1].Selected) || (index > 0 && !listBox.Items[i + 1].Selected))
                            {
                                ListItem itemA = listBox.Items[i]; //the selected item

                                listBox.Items.Remove(itemA); //is removed

                                listBox.Items.Insert(i + index, itemA);//and swapped
                            }
                        }
                    }
                }
            }
        }
 
Last edited by a moderator:
Back
Top