It appears (especially after reviewing the inc file) that the implementation of TListView, when sorting sorts the StringList - but not the actual elements which are visually rendered.
* How to reproduce: New Project, Add TTreeView, TListView, TStatusBar. Set TListView properties to ViewStyle=vsReport and Add Two Columns (0=FileName, 1=Size) save Unit1 as umainform - then paste the following code in and run it - when you double click on the lines in the TListView - you will notice the showmessage code shows the sorted value - however, the GUI widget is not sorted!
-- my source -- BuildTree (tried many different implementation - note this code works in Delphi fine - so the code logic works),
unit umainform;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, ComCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
ListView1: TListView;
StatusBar1: TStatusBar;
TreeView1: TTreeView;
procedure FormShow(Sender: TObject);
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
procedure ListView1DblClick(Sender: TObject);
procedure TreeView1DblClick(Sender: TObject);
private
{ private declarations }
procedure BuildTree(Path:String);
public
{ public declarations }
CurrentPath:String;
end;
var
Form1: TForm1;
implementation
uses
dxstring;
{ TForm1 }
procedure TForm1.BuildTree(Path:String);
Var
SRec:TSearchRec;
Err:Integer;
TreeNode:TTreeNode;
ListItem:TListItem;
begin
CurrentPath:=dxstring.AddBackSlash(Path);
Statusbar1.SimpleText:=CurrentPath;
TreeView1.SortType:=stNone;
TreeView1.Items.Clear;
TreeView1.BeginUpdate;
ListView1.SortType:=stNone;
ListView1.Items.Clear;
ListView1.BeginUpdate;
Err:=FindFirst(Statusbar1.SimpleText+'*',faAnyFile,SRec);
While Err=0 do begin
if (SRec.Attr and faDirectory)=faDirectory then begin
if (SRec.Name<>'.') then begin
{$IFDEF LINUX}
if (CurrentPath='/') and (SRec.Name='..') then continue;
{$ELSE}
if (Length(CurrentPath)=3) and (SRec.Name='..') then continue;
{$ENDIF}
TreeView1.Items.Add(Nil,SRec.Name);
end;
end
else begin
ListItem:=ListView1.Items.Add;
ListItem.Caption:=SRec.Name;
ListItem.SubItems.Add(IntToStr(SRec.Size));
end;
Err:=FindNext(SRec);
end;
FindClose(SRec);
ListView1.SortType:=stText; // causes sort to be called! {visually broken}
ListView1.EndUpdate;
TreeView1.SortType:=stText; // causes sort to be called! {works}
TreeView1.EndUpdate;
End;
procedure TForm1.FormShow(Sender: TObject);
Begin
ListView1.FlatScrollBars:=True;
ListView1.GridLines:=True;
BuildTree(ExtractFilePath(Paramstr(0)));
end;
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
begin
ListView1.SortColumn:=Column.Index;
end;
procedure TForm1.ListView1DblClick(Sender: TObject);
Var
StrList:TStringList;
begin
StrList:=TStringList.Create;
StrList.LoadFromFile(CurrentPath+ListView1.Selected.Caption);
ShowMessage(ExtractFileName(ListView1.Selected.Caption));
StrList.Free;
end;
procedure TForm1.TreeView1DblClick(Sender: TObject);
Const
{$IFDEF LINUX}
Slash = '/';
{$ELSE}
Slash = '\';
{$ENDIF}
begin
if TreeView1.Selected.Text='..' then begin
Delete(CurrentPath,Length(CurrentPath),1); // drop existing trailing slash
While (length(CurrentPath)>0) and
(Copy(CurrentPath,Length(CurrentPath),1)<>Slash) do
Delete(CurrentPath,Length(CurrentPath),1);
If CurrentPath='' then CurrentPath := Slash;
BuildTree(CurrentPath);
end
else begin
BuildTree(CurrentPath+TreeView1.Selected.Text);
end;
end;
initialization
{$I umainform.lrs}
end.