?
Solved

Sorting TListView

Posted on 2003-11-30
7
Medium Priority
?
544 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 640 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 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