After playing with the CheckedListBox a while, I am convinced you will find it much easier to implement this using two ListBoxes. The ListBox offers you built-in sorting by setting its 'Sorted Property to 'true.
If you still really wish to use a CheckedListBox, hopefully this code will give you a basis to work with.
Put two ListBoxes on a Form: set 'listBox2 to hold the set of all your possible selections; leave 'listBox1 empty. Set the 'Sorted Property of both ListBoxes to 'true.
private const int selectedItemLimit = 5;
private object currentItem;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("listBox1 index changed: " + listBox1.SelectedIndex.ToString());
currentItem = listBox1.SelectedItem;
if (currentItem == null) return;
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("listBox2 index changed: " + listBox1.SelectedIndex.ToString());
currentItem = listBox2.SelectedItem;
if (currentItem == null) return;
if (listBox1.Items.Count < selectedItemLimit)
{
}
}
As you work with this code, and, possibly, extend it, be sure and examine the output to the Console as you are developing.