whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

23 Visitors Online


 
...create a TListView which automatically resorts by column click?
Autor: Jeff D.
[ Print tip ]  

Tip Rating (4):  
     


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.

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners