...create a TListView which automatically resorts by column click?
|
Autor:
Jeff D. |
[ Print tip
] | | |
unit SortedListView;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls;
type
TSortedListView = class(TListView)
private
FSortColumn: Integer;
procedure SetSortColumn(const Value: Integer);
procedure SortedListViewCompare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer);
procedure SortedListViewColumnClick(Sender: TObject; Column: TListColumn);
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
{ Public declarations }
published
{ Published declarations }
property SortColumn: Integer read FSortColumn write SetSortColumn;
end;
implementation
{==============================================================================}
{ TSortedListView }
{==============================================================================}
constructor TSortedListView.Create(AOwner: TComponent);
begin
inherited;
OnColumnClick := SortedListViewColumnClick;
OnCompare := SortedListViewCompare;
end;
{==============================================================================}
procedure TSortedListView.SetSortColumn(const Value: Integer);
begin
FSortColumn := Value;
AlphaSort;
end;
{==============================================================================}
procedure TSortedListView.SortedListViewColumnClick(Sender: TObject;
Column: TListColumn);
begin
SortColumn := Column.Index;
end;
{==============================================================================}
procedure TSortedListView.SortedListViewCompare(Sender: TObject;
Item1, Item2: TListItem; Data: Integer; var Compare: Integer);
begin
if SortColumn = 0 then
Compare := CompareText(Item1.Caption, Item2.Caption)
else
Compare := CompareText(Item1.SubItems[SortColumn - 1], Item2.SubItems[SortColumn - 1]);
end;
end.
|