Moving and sorting numeric items between ListBox controls
How to maintain a sorted ListBox with numeric items, and move items between two ListBox controls.
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]);
}
}
}