Priyatna Codes - My Programming Journey
 


You are here: Home Delphi Stuffs SortListView
SortListView Component

Version: 1.01
Release Date: June 5th, 2007

Introduction

SortListView is a standard TListView descendant with sorting capability. It shows a little arrow in the currently sorted column. Clicking on this arrow will toggle ascending and descending order. You can use an event handler to perform a custom sort.

Being simple and straight forward, you can easily convert your standard listview to this component without modifying a single code, using text replacement tool or IDE expert.

 

Version History

Version 1.01 - June 5th, 2007
Bug fix when no SmallImages assigned to listview. Thanks to Bruce Christensen.

Version 1.00 - May 26th, 2007
Initial release

 

License

This library is released under Mozilla Public License. You can use it in your freeware, shareware or commercial softwares. You can send your modification to me, and if I decide to include it in the main distribution, I will add your name as a contributor. You can read full licensing information here.

 

Installation

There is no special process to install it on Delphi. Just open SortListViewD7.dpk on Delphi IDE and press Install button. I use Delphi 7. If you use another Delphi version, you may have to make some minor changes.

The component will appear in priyatna.org tab.

 

Properties

There are some main properties of the component:

  • property OnCustomSort: TSLVOnCustomSort;

    procedure (Sender: TObject; ColIndex: Integer; Str1, Str2: string; var Res: Integer; var Handled: Boolean) of object;

    This is an event that you can use to perform custom sort. ColIndex parameter is zero-based column number. Str1 and Str2 are strings to be compared.

    Res
    is a value -1, 0 or 1 that specifies the arrangement of those two strings. This value is usually returned by CompareText function. Set this variable to 0 if Str1 equals to Str2. Set to 1 if Str1 is greater than Str2 (Str1 comes after Str2), or -1 otherwise.

    Set Handled value to True to tell the component that you supplied Res value. Example usage:
    // sorting simple string. This is actually the default behavior of the component,
    // so you don't need to do this. I just want to show you how to fill Res value.
    procedure TForm1.SortListView1CustomSort(Sender: TObject; ColIndex: Integer; Str1, Str2: string; var Res: Integer; var Handled: Boolean); begin Res := CompareText(Str1, Str2); Handled := True; end; // sorting numbers procedure TForm1.SortListView1CustomSort(Sender: TObject; ColIndex: Integer; Str1, Str2: string; var Res: Integer; var Handled: Boolean); var n1, n2: Integer; begin // for example, third column contains numbers if (ColIndex = 2) then begin n1 := StrToIntDef(Str1, 0); n2 := StrToIntDef(Str2, 0); if (n1 = n2) then Res := 0 else if (n1 > n2) then Res := 1 else Res := -1; Handled := True; end; end;

 

Download

Download full source code and demo here:

SortListView.zip
260 KB

Version: 1.01
Release Date: June 5th, 2007