Click here to Skip to main content
5,787,682 members and growing! (18,884 online)
Email Password   helpLost your password?
Desktop Development » List Controls » List Controls     Beginner License: The Code Project Open License (CPOL)

Moving and sorting numeric items between ListBox controls

By dwanders

How to maintain a sorted ListBox with numeric items, and move items between two ListBox controls.
C#, Windows, .NET, Visual Studio, Dev

Posted: 17 Nov 2008
Updated: 17 Nov 2008
Views: 2,433
Bookmarked: 8 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
2 votes for this Article.
Popularity: 0.70 Rating: 2.33 out of 5
1 vote, 50.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
0 votes, 0.0%
4
1 vote, 50.0%
5

DemoMoveNumericItemsBetweenListBoxes

Introduction

This demo allows you to have a ListBox of available numeric items, and another ListBox of selected items. You can move items between the list boxes, and they will remain sorted. Using the Sorted property for the ListBox will not work properly when there are ten or more numeric items. This demo shows a way to keep the lists sorted.

Background

Having numeric items in a ListBox presents a problem when you want to sort the list. Setting the Sorted property to true will result in a alphabetically-sorted list. For example:

1
10
11
12
2
3
4
5
...

To get the items to sort properly, you can write a custom sort routine which overrides the Sort method. In my case, however, it was easy to get the items in sorted order from the database. The problem came when managing the list the user wanted to act upon.

This simple demo shows two ListBox controls. One lists the available items. The other shows the items selected by the user. The demo allows you to select items to move between the Available list and the Selected list. If nothing is selected, all items are moved.

The main routine receives the source listbox and the destination listbox as parameters. It will take the selected items from the source and add them to an integer array. Next, it takes the destination listbox items and adds those to the array. Then, it calls the Array.Sort method, clears the destination listbox items, and adds the items from the sorted array. Finally, it removes the elected items from the source listbox.

It's nothing fancy. And, you might want to override the Sort method for your needs. But, this seemed like a common scenario, and I was surprised there weren't any good examples already available. So, here you go.

Main routine

Here is the code that manages the lists. This is the MoveNumericItemsBetweenListBoxes() routine:

public void MoveNumericItemsBetweenListBoxes(ListBox SourceListBox, 
            ListBox DestinationListBox)
{
    // Move selected items from the source list box into the destination list box.
    // The destination list box will be sorted numerically.
    // If no items are selected, all items are moved.
    // Selected items are removed from the source list box.
    // To sort items numerically, an integer array is built
    // with selected items in the source list box, 
    // then items already in the destination list box are added to the array.
    // The array is sort and the destination list box is re-populated

    // create integer array for storing and sorting
    int ArraySize = DestinationListBox.Items.Count + 
                    SourceListBox.SelectedItems.Count;
    if (SourceListBox.SelectedItems.Count == 0)
    {
        ArraySize += SourceListBox.Items.Count;
    }
    int[] ItemList = new int[ArraySize];

    // add selected source items to array
    int OnItem = 0;
    if (SourceListBox.SelectedItems.Count == 0)
    {
        // nothing selected so copy all items
        for (OnItem = 0; OnItem < SourceListBox.Items.Count; OnItem++)
        {
            ItemList[OnItem] = (int)SourceListBox.Items[OnItem];
        }
    }
    else
    {
        // copy selected items
        for (OnItem = 0; OnItem < SourceListBox.SelectedItems.Count; OnItem++)
        {
            ItemList[OnItem] = (int)SourceListBox.SelectedItems[OnItem];
        }
    }

    // add current items in destination list to array
    int ArrayCount = OnItem;
    for (OnItem = 0; OnItem < DestinationListBox.Items.Count; OnItem++)
    {
        ItemList[ArrayCount++] = (int)DestinationListBox.Items[OnItem];
    }

    Array.Sort(ItemList);
    DestinationListBox.Items.Clear();

    // re-populate selected list
    for (OnItem = 0; OnItem < ArraySize; OnItem++)
    {
        DestinationListBox.Items.Add(ItemList[OnItem]);
    }

    // remove selected items from source list
    if (SourceListBox.SelectedItems.Count == 0)
    {
        SourceListBox.Items.Clear();
    }
    else
    {
        for ( önItem = SourceListBox.SelectedItems.Count - 1; OnItem >= 0; OnItem--)
        {
            SourceListBox.Items.RemoveAt(SourceListBox.SelectedIndices[OnItem]);
        }
    }
}

License

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

About the Author

dwanders



Location: United States United States

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
  (Refresh) 
-- There are no messages in this forum --

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Nov 2008
Editor: Smitha Vijayan
Copyright 2008 by dwanders
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project