MCattle
MCattle

Reputation: 3167

Natural Sorting a CheckedListbox

I've written the following class to create CheckedListboxes that use a Natural Sort algorithm. The overridden Sort() method, however, is not getting fired.

(The code within that method is from the MSDN example on how to implement your own sort algorithm on a regular Listbox.)

Public Class NaturalSortedCheckedListbox
   Inherits CheckedListBox

   Private _naturalComparer As New NaturalSortComparer

   Public Sub New()
      MyBase.new()
   End Sub

   Protected Overrides Sub Sort()
      '** A breakpoint on the following line will not get hit.
      If Items.Count > 1 Then
         Dim swapped As Boolean
         Do
            Dim counter As Integer = Items.Count - 1
            swapped = False

            While counter > 0
               If _naturalComparer.Compare(Items(counter).ToString(), Items(counter - 1).ToString()) = -1 Then
                  Dim temp As Object = Items(counter)
                  Items(counter) = Items(counter - 1)
                  Items(counter - 1) = temp
                  swapped = True
               End If

               counter -= 1
            End While
         Loop While swapped
      End If
   End Sub
End Class

A CheckedListbox derives from a Listbox, so I figured the Sort() override would work, but I'm stuck as to why it doesn't.

I am setting the instance's .Sorted = True, but it's just sorting items using the default algorithm, and not the Natural Sort algorithm (which has been tested and shown to work as expected elsewhere).

Upvotes: 2

Views: 2968

Answers (1)

Hans Passant
Hans Passant

Reputation: 942109

Yes, this is a "quirk", some call it a bug but pretty typical lossage with bolting .NET wrappers over native Windows controls. The problem is that the native Windows listbox control already implements sorting. When you set Sorted to True then the wrapper tells the control to start sorting any items that get added. The Sort() method got bolted on to allow the sort order to be customized, obviously useful.

Trouble is, that method only runs when you set Sorted to true. It doesn't call Sort() every time you add a new item. If Sorted was already true when you add an item then it is the native control that sorts. Obvious fail whale when you customized the order.

What you have to do is force the Sort() method to run again. That requires silly code like this:

    MyCheckedListBox1.Items.Add("sortdammit")
    MyCheckedListBox1.Sorted = False
    MyCheckedListBox1.Sorted = True

Beware that this is expensive so if you add multiple items then wait with toggling Sorted until you've added them all. It is the expense that caused the quirk.

Upvotes: 4

Related Questions