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)
{
int ArraySize = DestinationListBox.Items.Count +
SourceListBox.SelectedItems.Count;
if (SourceListBox.SelectedItems.Count == 0)
{
ArraySize += SourceListBox.Items.Count;
}
int[] ItemList = new int[ArraySize];
int OnItem = 0;
if (SourceListBox.SelectedItems.Count == 0)
{
for (OnItem = 0; OnItem < SourceListBox.Items.Count; OnItem++)
{
ItemList[OnItem] = (int)SourceListBox.Items[OnItem];
}
}
else
{
for (OnItem = 0; OnItem < SourceListBox.SelectedItems.Count; OnItem++)
{
ItemList[OnItem] = (int)SourceListBox.SelectedItems[OnItem];
}
}
int ArrayCount = OnItem;
for (OnItem = 0; OnItem < DestinationListBox.Items.Count; OnItem++)
{
ItemList[ArrayCount++] = (int)DestinationListBox.Items[OnItem];
}
Array.Sort(ItemList);
DestinationListBox.Items.Clear();
for (OnItem = 0; OnItem < ArraySize; OnItem++)
{
DestinationListBox.Items.Add(ItemList[OnItem]);
}
if (SourceListBox.SelectedItems.Count == 0)
{
SourceListBox.Items.Clear();
}
else
{
for ( önItem = SourceListBox.SelectedItems.Count - 1; OnItem >= 0; OnItem--)
{
SourceListBox.Items.RemoveAt(SourceListBox.SelectedIndices[OnItem]);
}
}
}
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.