unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
FMX.ListBox, FMX.Controls.Presentation, FMX.StdCtrls,
System.Generics.Collections;
type
TFoo =
class
private
FDate: TDate;
public
function ToString( ):
string;
override;
property Date: TDate
read FDate
write FDate;
end;
TFooList = TObjectList<TFoo>;
TForm1 =
class( TForm )
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button1Click( Sender: TObject );
procedure Button2Click( Sender: TObject );
procedure ListBox1Compare( Item1, Item2: TListBoxItem;
var Result: Integer );
private
FFooList: TFooList;
public
procedure AfterConstruction;
override;
procedure BeforeDestruction;
override;
end;
HelperForStrings =
class helper
for TStrings
public
function AddObject( AObject: TObject ): Integer;
overload;
procedure RefreshFromObjects( );
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses
System.DateUtils;
{ HelperForStrings }
function HelperForStrings.AddObject( AObject: TObject ): Integer;
begin
Result := Self.AddObject( AObject.ToString( ), AObject );
end;
procedure HelperForStrings.RefreshFromObjects;
var
idx: Integer;
begin
BeginUpdate( );
try
for idx := 0
to Count - 1
do
begin
if Objects[ idx ] <>
nil
then
Strings[ idx ] := Objects[ idx ].ToString( );
end;
finally
EndUpdate( );
end;
end;
{ TFoo }
function TFoo.ToString( ):
string;
begin
Result := DateToStr( FDate );
end;
procedure TForm1.AfterConstruction;
begin
inherited;
FFooList := TFooList.Create( True );
end;
procedure TForm1.BeforeDestruction;
begin
FFooList.Free;
inherited;
end;
procedure TForm1.Button1Click( Sender: TObject );
var
lFoo: TFoo;
begin
lFoo := TFoo.Create;
lFoo.Date := Date( ) + Random( 11 ) - 5;
FFooList.Add( lFoo );
ListBox1.Items.AddObject( lFoo );
end;
procedure TForm1.Button2Click( Sender: TObject );
var
idx : Integer;
foo : TFoo;
sorted: Boolean;
begin
idx := Random( FFooList.Count );
foo := FFooList[ idx ];
foo.Date := foo.Date + Random( 11 ) - 5;
sorted := ListBox1.sorted;
try
ListBox1.sorted := False;
ListBox1.Items.RefreshFromObjects( );
finally
ListBox1.sorted := sorted;
end;
end;
procedure TForm1.ListBox1Compare( Item1, Item2: TListBoxItem;
var Result: Integer );
begin
if Assigned( Item1.Data )
and Assigned( Item2.Data )
then
Result := CompareDate( TFoo( Item1.Data ).Date, TFoo( Item2.Data ).Date )
else
Result := 0;
end;
end.