Let the user arrange ListBox items in C#

[ListBox items]

This example demonstrates one way that you can let the user arrange ListBox items. To rearrange the items, the user can click on an item and then use the program’s buttons to move the item to the top of the list, up one position, down one position, or to the bottom of the list.

When the user selects an item, the following code enables and disables the appropriate buttons.

// Enable and disable the appropriate buttons.
private void lstAnimals_SelectedIndexChanged(object sender, EventArgs e)
{
    btnUp.Enabled = (lstAnimals.SelectedIndex > 0);
    btnToTop.Enabled = btnUp.Enabled;
    btnDown.Enabled =
        (lstAnimals.SelectedIndex < lstAnimals.Items.Count - 1);
    btnToBottom.Enabled = btnDown.Enabled;
}

If the selected index is greater than 0 (the first item has index 0), then the item is not at the top of the list. In that case, the code enables the Move Up and Move To Top buttons.

If the selected index is less than the number of items minus one (the last item has index lstAnimals.Items.Count - 1), then the item is not at the end of the list. In that case, the code enables the Move Down and Move To Bottom buttons.

After it moves the item, the button selects it. That is less confusing than leaving no item selected, it lets the user click the buttons multiple times to move the item repeatedly, and it ensures that the item remains visible if it moves off the bottom or top of the visible list.

The basic approach that each button uses to move the selected item is the same: save the item’s index if necessary, save the item, remove the item from the list box, and insert the item in its new position.

The following code shows how the buttons work.

// Move the selected item to the top of the list (index 0).
private void btnToTop_Click(object sender, EventArgs e)
{
    object item = lstAnimals.SelectedItem;
    lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex);
    lstAnimals.Items.Insert(0, item);
    lstAnimals.SelectedIndex = 0;
}

// Move the selected item up one position.
private void btnUp_Click(object sender, EventArgs e)
{
    int index = lstAnimals.SelectedIndex;
    object item = lstAnimals.SelectedItem;
    lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex);
    lstAnimals.Items.Insert(index - 1, item);
    lstAnimals.SelectedIndex = index - 1;
}

// Move the selected item down one position.
private void btnDown_Click(object sender, EventArgs e)
{
    int index = lstAnimals.SelectedIndex;
    object item = lstAnimals.SelectedItem;
    lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex);
    lstAnimals.Items.Insert(index + 1, item);
    lstAnimals.SelectedIndex = index + 1;
}

// Move the selected item to the end of the list.
private void btnToBottom_Click(object sender, EventArgs e)
{
    object item = lstAnimals.SelectedItem;
    lstAnimals.Items.RemoveAt(lstAnimals.SelectedIndex);
    lstAnimals.Items.Add(item);
    lstAnimals.SelectedIndex = lstAnimals.Items.Count - 1;
}

That’s all there is to it. This method is cumbersome if you want to move ListBox items a long way through the list, but it’s easy enough if the list is short or if the user doesn’t need to move items very far.


Download Example   Follow me on Twitter   RSS feed   Donate




About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in controls, user interface and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.