unit Form.Main;
interface
uses
Winapi.Windows,
Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics,
Vcl.Controls,
Vcl.Forms,
Vcl.Dialogs,
Vcl.ComCtrls,
Vcl.StdCtrls;
type
TForm1 =
class( TForm )
ListView1: TListView;
ListView2: TListView;
SaveDialog1: TSaveDialog;
OpenDialog1: TOpenDialog;
Button1: TButton;
Button2: TButton;
procedure Button1Click( Sender: TObject );
procedure Button2Click( Sender: TObject );
private
function Serialize( ListView: TListView ):
string;
overload;
function Serialize( ListItems: TListItems ):
string;
overload;
function Serialize( ListItem: TListItem ):
string;
overload;
procedure Deserialize(
const Data:
string; ListView: TListView );
overload;
procedure Deserialize(
const Data:
string; ListItems: TListItems );
overload;
procedure Deserialize(
const Data:
string; ListItem: TListItem );
overload;
procedure SaveToFile(
const AFileName:
string );
procedure LoadFromFile(
const AFileName:
string );
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.Button2Click( Sender: TObject );
begin
if SaveDialog1.Execute
then
SaveToFile( SaveDialog1.FileName );
end;
procedure TForm1.SaveToFile(
const AFileName:
string );
var
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
LStrings.Values['
ListView1'] := Serialize( ListView1 );
LStrings.Values['
ListView2'] := Serialize( ListView2 );
LStrings.SaveToFile( AFileName );
finally
LStrings.Free;
end;
end;
function TForm1.Serialize( ListView: TListView ):
string;
var
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
LStrings.Values['
Items'] := Serialize( ListView.Items );
Result := LStrings.DelimitedText;
finally
LStrings.Free;
end;
end;
function TForm1.Serialize( ListItems: TListItems ):
string;
var
LIdx: Integer;
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
for LIdx := 0
to ListItems.Count - 1
do
LStrings.Add( Serialize( ListItems[LIdx] ) );
Result := LStrings.DelimitedText;
finally
LStrings.Free;
end;
end;
function TForm1.Serialize( ListItem: TListItem ):
string;
var
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
LStrings.Values['
Caption'] := ListItem.Caption;
LStrings.Values['
SubItems'] := ListItem.SubItems.DelimitedText;
Result := LStrings.DelimitedText;
finally
LStrings.Free;
end;
end;
procedure TForm1.Button1Click( Sender: TObject );
begin
if OpenDialog1.Execute
then
LoadFromFile( OpenDialog1.FileName );
end;
procedure TForm1.LoadFromFile(
const AFileName:
string );
var
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
LStrings.LoadFromFile( AFileName );
Deserialize( LStrings.Values['
ListView1'], ListView1 );
Deserialize( LStrings.Values['
ListView2'], ListView2 );
finally
LStrings.Free;
end;
end;
procedure TForm1.Deserialize(
const Data:
string; ListView: TListView );
var
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
LStrings.DelimitedText := Data;
Deserialize( LStrings.Values['
Items'], ListView.Items );
finally
LStrings.Free;
end;
end;
procedure TForm1.Deserialize(
const Data:
string; ListItems: TListItems );
var
LIdx: Integer;
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
LStrings.DelimitedText := Data;
ListItems.Clear;
for LIdx := 0
to LStrings.Count - 1
do
Deserialize( LStrings[LIdx], ListItems.Add );
finally
LStrings.Free;
end;
end;
procedure TForm1.Deserialize(
const Data:
string; ListItem: TListItem );
var
LStrings: TStrings;
begin
LStrings := TStringList.Create;
try
LStrings.DelimitedText := Data;
ListItem.Caption := LStrings.Values['
Caption'];
ListItem.SubItems.DelimitedText := LStrings.Values['
SubItems'];
finally
LStrings.Free;
end;
end;
end.