A rare, but potentially nasty, case to beware of:

  // Does not work
  procedure TForm1.SortListBox(AListBox: TListBox);
  begin
    AListBox.Sorted := True; // to sort the contents
    AListBox.Sorted := False; // eg. to allow drag/dropping of items
  end;

The code as written doesn't work.  Setting Sorted results in a call to
DestroyHandle, and nothing happens until the handle is recreated.  By the
time the handle is recreated, Sorted is false again.  So no sorting takes
place.

The solution is either to access some property of the listbox, almost all of
which will internally call HandleNeeded, or (best) call HandleNeeded
explicitly, ie.

  // Works
  procedure TForm1.SortListBox(AListBox: TListBox);
  begin
    AListBox.Sorted := True; // state intention to sort contents
    AListBox.HandleNeeded; // sorting actually occurs now
    AListBox.Sorted := False;
  end;

This was a hard problem to find because if you had a watch on, for example,
AListBox.Items.Text, the mere reference to it in the watch window would
cause the handle to be created and everything to work - only if you stepped
over the "Sorted := True" line.  Aaargh!  :)

Cheers,
Carl
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to