Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move item in listBox up and down?

People also ask

How do I select an item in ListBox?

To select an item in a ListBox, we can use the SetSelect method that takes an item index and a true or false value where the true value represents the item to be selected. The following code snippet sets a ListBox to allow multiple selection and selects the second and third items in the list: listBox1.

How do I select multiple items in ListBox?

Choose Multiple Items from ListboxOn the worksheet, click on a cell that has a drop down list. The VBA listbox pops up automatically, and shows all the choices from the cell's drop down list. Add a check mark to one or more of the items in the list box. When you're finished selecting items, click the OK button.


 public void MoveUp()
 {
     MoveItem(-1);
 }

 public void MoveDown()
 {
    MoveItem(1);
 }

 public void MoveItem(int direction)
 {
    // Checking selected item
    if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0)
        return; // No selected item - nothing to do

    // Calculate new index using move direction
    int newIndex = listBox1.SelectedIndex + direction;

    // Checking bounds of the range
    if (newIndex < 0 || newIndex >= listBox1.Items.Count)
        return; // Index out of range - nothing to do

    object selected = listBox1.SelectedItem;

    // Removing removable element
    listBox1.Items.Remove(selected);
    // Insert it in new position
    listBox1.Items.Insert(newIndex, selected);
    // Restore selection
    listBox1.SetSelected(newIndex, true);
}

UPD 2020-03-24: Extension class for simple reuse and it also supports CheckedListBox (if CheckedListBox is not needed for you, please remove appropriate lines of code). Thanks @dognose and @Chad

public static class ListBoxExtension
{
    public static void MoveSelectedItemUp(this ListBox listBox)
    {
        _MoveSelectedItem(listBox, -1);
    }

    public static void MoveSelectedItemDown(this ListBox listBox)
    {
        _MoveSelectedItem(listBox, 1);
    }

    static void _MoveSelectedItem(ListBox listBox, int direction)
    {
        // Checking selected item
        if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
            return; // No selected item - nothing to do

        // Calculate new index using move direction
        int newIndex = listBox.SelectedIndex + direction;

        // Checking bounds of the range
        if (newIndex < 0 || newIndex >= listBox.Items.Count)
            return; // Index out of range - nothing to do

        object selected = listBox.SelectedItem;

        // Save checked state if it is applicable
        var checkedListBox = listBox as CheckedListBox;
        var checkState = CheckState.Unchecked;
        if (checkedListBox != null)
            checkState = checkedListBox.GetItemCheckState(checkedListBox.SelectedIndex);

        // Removing removable element
        listBox.Items.Remove(selected);
        // Insert it in new position
        listBox.Items.Insert(newIndex, selected);
        // Restore selection
        listBox.SetSelected(newIndex, true);

        // Restore checked state if it is applicable
        if (checkedListBox != null)
            checkedListBox.SetItemCheckState(newIndex, checkState);
    }
}

private void UpClick()
{
    // only if the first item isn't the current one
    if(listBox1.ListIndex > 0)
    {
        // add a duplicate item up in the listbox
        listBox1.AddItem(listBox1.Text, listBox1.ListIndex - 1);
        // make it the current item
        listBox1.ListIndex = (listBox1.ListIndex - 2);
        // delete the old occurrence of this item
        listBox1.RemoveItem(listBox1.ListIndex + 2);
    }
}

private void DownClick()
{
   // only if the last item isn't the current one
   if((listBox1.ListIndex != -1) && (listBox1.ListIndex < listBox1.ListCount - 1))
   {
      // add a duplicate item down in the listbox
      listBox1.AddItem(listBox1.Text, listBox1.ListIndex + 2);
      // make it the current item
      listBox1.ListIndex = listBox1.ListIndex + 2;
      // delete the old occurrence of this item
      listBox1.RemoveItem(listBox1.ListIndex - 2);
   }
}

Did you try searching it in google? Move Items up/dowm in listbox control for example.

public class SmartListBox : ListBox
{
    //Moves the selected items up one level
    public MoveUp()
    {

        for(int i = 0; i < Items.Count; i++)
        {
            if (Items[i].Selected)//identify the selected item
            {
                //swap with the top item(move up)
                if (i > 0 && !Items[i - 1].Selected)
                {
                     ListItem bottom = Items[i];
                     Items.Remove(bottom);
                     Items.Insert(i - 1, bottom);
                     Items[i - 1].Selected = true;
                 }
              }
          }
     }
     //Moves the selected items one level down
     public MoveDown()
     {
         int startindex = Items.Count -1;
         for (int i = startindex; i > -1; i--)
         {
              if (Items[i].Selected)//identify the selected item
              { 
                  //swap with the lower item(move down)
                  if (i < startindex && !Items[i + 1].Selected)
                  {
                       ListItem bottom = Items[i];
                       Items.Remove(bottom);
                       Items.Insert(i + 1, bottom);
                       Items[i + 1].Selected = true;
                  }

              }
         }
     }
}

Modified @Save code to allow for moving items that are data bound to a ListBox using DataSource property.

public void MoveItem(int direction)
        {
            // Checking selected item
            if (listBox1.SelectedItem == null || listBox1.SelectedIndex < 0)
                return; // No selected item - nothing to do

            // Calculate new index using move direction
            int newIndex = listBox1.SelectedIndex + direction;

            // Checking bounds of the range
            if (newIndex < 0 || newIndex >= listBox1.Items.Count)
                return; // Index out of range - nothing to do

            UnifyCamera selected = listBox1.SelectedItem as UnifyCamera;

            // modify the data source list
            inputData.Cameras.RemoveAt(listBox1.SelectedIndex);
            inputData.Cameras.Insert(newIndex, selected);

            // re-bind your data source
            ((ListBox)listBox1).DataSource = null;
            ((ListBox)listBox1).DataSource = this.inputData.Cameras;
            ((ListBox)listBox1).DisplayMember = "Name";

            // Restore selection
            listBox1.SetSelected(newIndex, true);
        }

Where UnifyCamera is my custom class that is stored in a list inputData.Cameras that returns a List<UnifyCamera>.


Modified Desolator code above to pass the control as a parameter...reusable

    private void MoveUp()
    {
        MoveItem(-1,listBox1);
    }      


    private void MoveDown()
    {
        MoveItem(1,listBox1);
    }

    public void MoveItem(int direction,ListBox listBox)
    {
        // Checking selected item
        if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
            return; // No selected item - nothing to do

        // Calculate new index using move direction
        int newIndex = listBox.SelectedIndex + direction;

        // Checking bounds of the range
        if (newIndex < 0 || newIndex >= listBox.Items.Count)
            return; // Index out of range - nothing to do

        object selected = listBox.SelectedItem;

        // Removing removable element
        listBox.Items.Remove(selected);
        // Insert it in new position
        listBox.Items.Insert(newIndex, selected);
        // Restore selection
        listBox.SetSelected(newIndex, true);
    }