Click here to Skip to main content
11,642,233 members (63,356 online)
Click here to Skip to main content

Moving listbox items

, 26 Mar 2008 CPOL 42K 1.3K 13
Rate this:
Please Sign up or sign in to vote.
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:

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:

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)

Share

About the Author

privately voided()
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Member 277166629-Oct-13 16:38
memberMember 277166629-Oct-13 16:38 
Questionfollowing is smart code without any trouble: Pin
Jai Deo Tiwari22-Oct-13 21:21
memberJai Deo Tiwari22-Oct-13 21:21 
GeneralMy vote of 5 Pin
viragdesai16-Sep-12 18:58
memberviragdesai16-Sep-12 18:58 
GeneralGreat Code Pin
tim horigan20-Feb-11 9:38
membertim horigan20-Feb-11 9:38 
GeneralMy vote of 5 Pin
tim horigan20-Feb-11 9:37
membertim horigan20-Feb-11 9:37 
GeneralMy vote of 2 Pin
ReymonARG13-Feb-10 18:48
memberReymonARG13-Feb-10 18:48 
GeneralSlight issue with this example if duplicates exist in your list. Pin
tooMuchCode6-Nov-08 12:26
membertooMuchCode6-Nov-08 12:26 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 26 Mar 2008
Article Copyright 2008 by privately voided()
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid