Board index » delphi » TListView sorting

TListView sorting

Folks,

I have a TListView in vsReport style, and I need to be able to sort it
whenever the user clicks on one of the header columns.

If he clicks on the first column, it's pretty easy: just set the
SortType to stText - according to the help, this sorts on the list
item's caption.

If he clicks on a different column, it's a bit more involved, I need
to set SortType to stData, and I need to have a OnCompare handler - no
biggie, that works nicely, too.

But now for the problem: if I have a TListView, and I have SortType =
stText in the beginning, everything's fine. If I click on the column I
want to sort it by, everything works nicely. But when I try to go back
to the "sort by caption", by setting SortType to stText, it doesn't
work anymore....

It seems that *IF* there is an OnCompare handler set, it will ALWAYS
call that handler - contrary to what the help says:

"An OnCompare event handler is called when the SortType property is
stData or stBoth, when the AlphaSort method is called, or when the
CustomSort method is called without a SortProc parameter."

As I understand this paragraph, when the SortType is set to stText,
the OnCompare handler should NOT be called, right? Well, it is being
called....

Any ideas??? Is this a known bug / behaviour?

Thanks!
Marc

------------------------------------------------------------------------
Marc Scheuner                                          Software Engineer
FastLane Technologies Inc.                  Halifax, Nova Scotia, Canada
Email: mailto:mscheu...@fastlane.com

Plan, Deploy, Manage Windows 2000 with FastLane DM/Suite.
Optimize Windows 2000 now!
http://www.fastlane.com

 

Re:TListView sorting


On Thu, 03 Aug 2000 10:05:33 -0300, Marc Scheuner

Quote
<mscheu...@fastlane.com> wrote:
>As I understand this paragraph, when the SortType is set to stText,
>the OnCompare handler should NOT be called, right? Well, it is being
>called....

>Any ideas??? Is this a known bug / behaviour?

Just pass it on to a compare function, i guess AnsiStrComp would be
nice

- Asbj?rn

Re:TListView sorting


On Thu, 03 Aug 2000 13:57:16 GMT, lord...@hotmail.com (Lord Crc)
wrote:

Quote
>>As I understand this paragraph, when the SortType is set to stText,
>>the OnCompare handler should NOT be called, right? Well, it is being
>>called....

>Just pass it on to a compare function, i guess AnsiStrComp would be
>nice

??? According to the docs, if I set SortType to stText, the OnCompare
function will NOT be called - that's what stumps me - because it is
being called all the time........

Is that behaviour correct (i.e. is the documentation wrong?), or is
something else happening here.......

Marc

------------------------------------------------------------------------
Marc Scheuner                                          Software Engineer
FastLane Technologies Inc.                  Halifax, Nova Scotia, Canada
Email: mailto:mscheu...@fastlane.com

Plan, Deploy, Manage Windows 2000 with FastLane DM/Suite.
Optimize Windows 2000 now!
http://www.fastlane.com

Re:TListView sorting


On Thu, 03 Aug 2000 16:11:23 -0300, Marc Scheuner

Quote
<mscheu...@no.spam.for.me.ca> wrote:
>On Thu, 03 Aug 2000 13:57:16 GMT, lord...@hotmail.com (Lord Crc)
>wrote:
>>>As I understand this paragraph, when the SortType is set to stText,
>>>the OnCompare handler should NOT be called, right? Well, it is being
>>>called....

>>Just pass it on to a compare function, i guess AnsiStrComp would be
>>nice

>??? According to the docs, if I set SortType to stText, the OnCompare
>function will NOT be called - that's what stumps me - because it is
>being called all the time........

>Is that behaviour correct (i.e. is the documentation wrong?), or is
>something else happening here.......

Let's take a little look at the code:

procedure TCustomListView.SetSortType(Value: TSortType);
begin
  if SortType <> Value then
  begin
    FSortType := Value;
    if ((SortType in [stData, stBoth]) and Assigned(OnCompare)) or
      (SortType in [stText, stBoth]) then
      AlphaSort;
  end;
end;

So, if the SortType is set to stText, AlphaSort is called (uhm, isn't
it called no matter what?) Anyway, let's look at AlphaSort:

function TCustomListView.AlphaSort: Boolean;
begin
  if HandleAllocated then
    Result := ListView_SortItems(Handle, @DefaultListViewSort, 0)
  else Result := False;
end;
Ok, nothing fancy there calling the API, so lets look at the
DefaultListViewSort, which takes care of the sorting:

function DefaultListViewSort(Item1, Item2: TListItem;
  lParam: Integer): Integer; stdcall;
begin
  with Item1 do
    if Assigned(ListView.OnCompare) then
      ListView.OnCompare(ListView, Item1, Item2, lParam, Result)
    else Result := lstrcmp(PChar(Item1.Caption),
PChar(Item2.Caption));
end;

Which is doing exactly what the help says about AlphaSort:

"An OnCompare event handler is called when the SortType property is
stData or stBoth, when the AlphaSort method is called, or when the
CustomSort method is called without a SortProc parameter."

The only problem here, is that the help is wrong about stText. stText
will only sort itself IF OnCompare is NOT assigned.

Uhm, at least thats i think :)

- Asbj?rn

Other Threads