uses ...,
Vcl.ComCtrls, System.Types, System.StrUtils;
procedure ListViewSaveToFile(ListView: TListView;
const FileName:
string);
procedure AddTextToLine(
var Line:
string;
const Text:
string);
begin
Line := Line + Text + #9;
end;
procedure MoveCompletedLineToList(
const Strings: TStringList;
var Line:
string);
begin
Strings.Add(System.Copy(Line, 1, Length(Line)-1));
//remove trailing tab
Line := '
';
end;
var
Strings: TStringList;
LatestLine:
string;
i, j: Integer;
begin
LatestLine := '
';
Strings := TStringList.Create;
try
for i := 0
to ListView.Items.Count-1
do begin
AddTextToLine(LatestLine, ListView.Items[i].Caption);
for j := 0
to ListView.Items[i].SubItems.Count-1
do begin
AddTextToLine(LatestLine, ListView.Items[i].SubItems[j]);
end;
MoveCompletedLineToList(Strings, LatestLine);
end;
Strings.SaveToFile(FileName, TEncoding.UTF8);
finally
Strings.Free;
end;
end;
procedure ListViewLoadFromFile(ListView: TListView;
const FileName:
string);
var
Strings: TStringList;
i, j: Integer;
Fields: TStringDynArray;
Item: TListItem;
begin
Strings := TStringList.Create;
try
Strings.LoadFromFile(FileName);
ListView.Clear;
for i := 0
to Strings.Count-1
do begin
Fields := SplitString(Strings[i], #9);
Item := ListView.Items.Add;
Item.Caption := Fields[0];
for j := 1
to high(Fields)
do begin
Item.SubItems.Add(Fields[j]);
end;
end;
finally
Strings.Free;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ListViewSaveToFile(ListView1,'
D:\LV1.txt');
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ListViewLoadFromFile(ListView1,'
D:\LV1.txt');
end;