Solved

Sorting TListView

Posted on 2003-11-30
7
536 Views
Last Modified: 2010-04-05
Hello, I would sort a TListView by Clicking on the column (containes DateTime). Could You help me there?
0
Comment
Question by:k4hvd77
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:sftweng
ID: 9845662
The first part is to determine which column the mouse is in. Try something like this:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    StatusBar1: TStatusBar;
    procedure ListView1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListView1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  StatusBar1.Panels[0].Text := IntToStr(x);
  StatusBar1.Panels[1].Text := IntToStr(y);
  IF (X > ListView1.Columns[0].Width)
  THEN StatusBar1.Panels[2].Text := 'Column 2'
  ELSE StatusBar1.Panels[2].Text := 'Column 1';
end;

end.
0
 
LVL 7

Expert Comment

by:sftweng
ID: 9845681
Of course a better way to get the column is to use an OnColumnClick event.

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
begin

end;
0
 
LVL 4

Author Comment

by:k4hvd77
ID: 9845693
but How can I sort it?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 7

Expert Comment

by:sftweng
ID: 9845703
Do you want to sort by Caption, Data or Both?
0
 
LVL 4

Author Comment

by:k4hvd77
ID: 9845795
I would sort by caption (Caption contains TDateTime)
0
 
LVL 7

Accepted Solution

by:
sftweng earned 160 total points
ID: 9846200
OK, here's a framework for sorting, built on the TListView example in the help files:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ImgList, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    ImageList1: TImageList;
    ComboBox1: TComboBox;
    StatusBar1: TStatusBar;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Click(Sender: TObject);
    procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
    procedure ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
      Data: Integer; var Compare: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  initialized : BOOLEAN;

implementation

{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  ListItem: TListItem;
  NewColumn: TListColumn;
begin
  // Create a ListView item for each image in the ImageList
  initialized := FALSE;
  with ListView1 do
  begin
    SmallImages := ImageList1;
    LargeImages := ImageList1;
    for I := 0 to ImageList1.Count - 1 do
    begin
      ListItem := Items.Add;
      Listitem.Caption := 'Image' + IntToStr(I);
      ListItem.SubItems.Add(DateTimeToStr(Now));
      ListItem.ImageIndex := I;
    end;
    // Create two columns to show during viewing as vsReport
//    NewColumn := Columns.Add;
//    NewColumn.Caption := 'Column 1';
//    NewColumn := Columns.Add;
//    NewColumn.Caption := 'Column 2';
    // Add View styles and constants to the Combo Box
    ComboBox1.Items.AddObject('vsIcon', TObject(vsIcon));
    ComboBox1.Items.AddObject('vsList', TObject(vsList));
    ComboBox1.Items.AddObject('vsReport', TObject(vsReport));
    ComboBox1.Items.AddObject('vsSmallIcon', TObject(vsSmallIcon));
    // Display first item in the Combo Box
    ComboBox1.ItemIndex := 0;
  end;
  initialized := TRUE;
end;

procedure TForm1.ComboBox1Click(Sender: TObject);
begin
  with ComboBox1 do
    ListView1.ViewStyle := TViewStyle(Items.Objects[ItemIndex]);
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
  Column: TListColumn);
VAR
  colID : INTEGER;
  colIndex : INTEGER;
begin
  colID := Column.ID;
  colIndex := Column.Index;
  StatusBar1.Panels[0].Text := 'ID:'+IntToStr(ColID);
  StatusBar1.Panels[1].Text := 'IX:'+IntToStr(ColIndex);
  ListView1.AlphaSort;
end;

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);
VAR
  ax, bx : INTEGER;
  aVal, bVal : String;
begin
  IF initialized
  THEN BEGIN
    ax := Item1.Index; bx := Item2.Index;
    aVal := Item1.SubItems[0];
    bVal := Item2.SubItems[0];
    StatusBar1.Panels[2].Text := 'AV:'+aVal;
    StatusBar1.Panels[3].Text := 'BV:'+bVal;
  END {IF};
end;

end.

In the ListView1Compare event handler, you'll need to convert the string representation of the TDateTimes back into a TDateTime for the two ListItems to be compared (compare them as TDateTimes rather than strings), and return a value indicating whether a < b, a = b, or a > b.

Let me know if this doesn't get you going.
0
 
LVL 4

Author Comment

by:k4hvd77
ID: 9846242
Exact that what I want :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now