Re: Sorting Delphi's ShellListview by clicking column


[ DelphiLand FAQ ]

Posted by webmaster Guido on November 17, 2004 at 22:41:47:

In Reply to: Sorting ShellListview by clicking column posted by creeek on November 17, 2004 at 12:57:41:

: ... sort a ShellListView by clicking a column header (date, type, name)?

-------------------------------

From Delphi 6 onwards, some so-called "shell controls" are included (TShellListView,...) that you can use to simulate the functionality of Windows Explorer. Usually, these Delphi components are located on the Samples page of the component palette. But alas, there is no documentation from Borland...

If you have the Delphi source code, you can look for ShellCtrls.pas and modify it as indicated below. Afterwards, remove the shell controls, recompile and install them again.

Note 1: the information below is compiled from an article in a Borland newsgroup, with thanks to author Jim Kueneman.
Note 2: before attempting this trick, make sure to make a backup of the original ShellCtrls.pas! :)

To change in ShellCtrls.pas:

---------------------------
TCustomShellListView = class(TCustomListView, IShellCommandVerb)
...
public
...
  // Add the following line:
  property FolderList: TList read FFolders; 
...
end;
---------------------------

How to use your modified ShellListView?
Let's assume that ShellListView1 is a TShellListView component on Form1. Proceed as follows:

1. In the implementation section of Form1, add a global variable SortColumn of type integer.
2. In the implementation section of Form1, add a function ShellCompare as indicated in the source code below.
3. Create an OnColumnClick for ShellListView1 and add code as in the code example below.

------------------------
unit Unit1;
  
interface
  ...
  type
    TForm1 = class(TForm)
      ShellListView1: TShellListView;
      ...
      procedure ShellListView1ColumnClick(Sender: TObject;
        Column: TListColumn);
      ...
    private
      { Private declarations }
    public
     { Public declarations }
    end;
    
var
  Form1: TForm1;
     
implementation
{$R *.dfm}
     
var
  SortColumn: Integer;
     
function ShellCompare(Item1, Item2: Pointer): Integer;
begin
  Result := Smallint(
      TShellFolder(Item1).ParentShellFolder.CompareIDs(
      SortColumn,
      TShellFolder(Item1).RelativeID,
      TShellFolder(Item2).RelativeID) );
end;
     
procedure TForm1.ShellListView1ColumnClick(Sender: TObject;
 Column: TListColumn);
begin
  SortColumn := Column.Index;
  ShellListView1.FolderList.Sort(ShellCompare);
  ShellListView1.Invalidate;
end;
     
//...your code
//...
---------------------

Can you please let us know if it worked, or if you encountered problems with this code?

Good luck!
Guido, DelphiLand Team




Related Articles and Replies


[ Delphi Forum -- by DelphiLand ]