Click here to Skip to main content
15,889,034 members
Articles / Programming Languages / C#

Moving listbox items

Rate me:
Please Sign up or sign in to vote.
3.71/5 (11 votes)
26 Mar 2008CPOL 67.3K   1.7K   13   8
Move lisbox items and change indices.

Introduction

This solution helps with a small but sometimes irritating problem, which may cause time loss when working on large projects. It moves listbox items up and down on the click of a button.

Using the code

First off, add a button or two, depending on what you want to achieve, to your form and set this code to the up button action:

C#
if (listBox1.SelectedItems.Count > 0)
{
    object selected = listBox1.SelectedItem;
    int indx = listBox1.Items.IndexOf(selected);
    int totl = listBox1.Items.Count;

    if (indx == 0)
    {
        listBox1.Items.Remove(selected);
        listBox1.Items.Insert(totl - 1, selected);
        listBox1.SetSelected(totl - 1, true);
    }
    else{
        listBox1.Items.Remove(selected);
        listBox1.Items.Insert(indx - 1, selected);
        listBox1.SetSelected(indx - 1, true);
    }
}

And then, add this to the down button action:

C#
if (listBox1.SelectedItems.Count > 0)
{
    object selected = listBox1.SelectedItem;
    int indx = listBox1.Items.IndexOf(selected);
    int totl = listBox1.Items.Count;

    if (indx == totl - 1)
    {
        listBox1.Items.Remove(selected);
        listBox1.Items.Insert(0, selected);
        listBox1.SetSelected(0, true);
    }
    else{
        listBox1.Items.Remove(selected);
        listBox1.Items.Insert(indx + 1, selected);
        listBox1.SetSelected(indx + 1, true);
    }
}

Points of Interest

For even better usability, the buttons can be disabled by default, and can be enabled when an item is selected. Furthermore, I hope this code will come in handy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerIt is Moving listbox not items but item Pin
Jeong, WonYoung(Brad)7-Sep-15 20:52
professionalJeong, WonYoung(Brad)7-Sep-15 20:52 
GeneralMy vote of 4 Pin
Member 277166629-Oct-13 16:38
Member 277166629-Oct-13 16:38 
Questionfollowing is smart code without any trouble: Pin
Jai Deo Tiwari22-Oct-13 21:21
Jai Deo Tiwari22-Oct-13 21:21 
GeneralMy vote of 5 Pin
viragdesai16-Sep-12 18:58
viragdesai16-Sep-12 18:58 
GeneralGreat Code Pin
tim horigan20-Feb-11 9:38
tim horigan20-Feb-11 9:38 
GeneralMy vote of 5 Pin
tim horigan20-Feb-11 9:37
tim horigan20-Feb-11 9:37 
GeneralMy vote of 2 Pin
ReymonARG13-Feb-10 18:48
ReymonARG13-Feb-10 18:48 
GeneralSlight issue with this example if duplicates exist in your list. Pin
tooMuchCode6-Nov-08 12:26
tooMuchCode6-Nov-08 12:26 
This code does works fine as long as all of the values in the list are completely unique. However if you have more than one item in the list with the same text you will get unexpected results as the item removed isn't always the item you selected it may be the other copy of the same text. I found this out the hard way and after stumbling around for a while I figured out that if there is a chance of duplicate text you should use the ListBox1.RemoveAt(<desired index="">);.

Here is the complete code I used: (Similar approach as this article illustrates but slightly different.)

private void btnMoveUp_Click(object sender, EventArgs e)
{
//Makes sure item is selected or else bypasses procedure to prevent error.
if (ListBox1.SelectedIndex >= 0)
{
//record orginal location and text.
Int32 iSelectedIndex = ListBox1.SelectedIndex;
string sSelectedItem = ListBox1.SelectedItem.ToString();

//determine new location.
Int32 iNewIndex = (iSelectedIndex - 1 => 0 ? 0 : iSelectedIndex - 1);

//remove item by index number since index number will always be unique.
ListBox1.Items.RemoveAt(iSelectedIndex);
//insert item with new index and orignal text .
ListBox1.Items.Insert(iNewIndex, sSelectedItem);

//Select item in new location.
ListBox1.SelectedIndex = iNewIndex;
}
}

private void btnMoveDown_Click(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex >= 0)
{
Int32 iSelectIndex = ListBox1.SelectedIndex;
string sSelectedItem = ListBox1.SelectedItem.ToString();

Int32 iUpperLimit = ListBox1.Items.Count - 1;

Int32 iNewIndex = (iSelectIndex + 1 >= iUpperLimit ? iUpperLimit : iSelectIndex + 1);

ListBox1.Items.RemoveAt(iSelectIndex);
ListBox1.Items.Insert(iNewIndex, sSelectedItem);

ListBox1.SelectedIndex = iNewIndex;
}
}

I hope may example can save someone the hour of frustration and swearing I just went through. You all have a good day!

tooMuchCode
"sometimes when I close my eyes the code comes out of the shower head"

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.