Hei, håper dette er til hjelp
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
btnAddItem: TButton;
btnSave: TButton;
btnLoad: TButton;
procedure btnAddItemClick(Sender: TObject);
procedure btnLoadClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TItemRec = record
Caption: array [0..99] of Char;
SubItems: array of array [0..99] of Char;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnAddItemClick(Sender: TObject);
begin
with ListView1.Items.Add do
begin
Caption := 'caption';
SubItems.Add('subitem1');
SubItems.Add('subitem2');
end;
end;
procedure SaveListViewItems(FileName: string; var ListView: TListView);
var
ItemRec: TItemRec;
iItemsCount,
iSubItemsCount: Integer;
Stream: TFileStream;
s: String;
iNumSubItems: Integer;
begin
Stream := TFileStream.Create(FileName, fmCreate);
with Stream do
try
for iItemsCount := 0 to ListView.Items.Count - 1 do
begin
FillChar(ItemRec.Caption, Length(ItemRec.Caption), 0);
s := ListView.Items.Item[iItemsCount].Caption;
Move(s[1],
ItemRec.Caption,
Length(s));
Write(ItemRec.Caption, Length(ItemRec.Caption));
iNumSubItems := ListView.Items.Item[iItemsCount].SubItems.Count;
Write(iNumSubItems, SizeOf(Integer));
for iSubItemsCount := 0 to ListView.Items.Item[iItemsCount].SubItems.Count - 1 do
begin
SetLength(ItemRec.SubItems, iSubItemsCount + 1);
FillChar(ItemRec.SubItems[iSubItemsCount], Length(ItemRec.SubItems[iSubItemsCount]), 0);
s := ListView.Items.Item[iItemsCount].SubItems[iSubItemsCount];
Move(s[1],
ItemRec.SubItems[iSubItemsCount],
Length(s));
Write(ItemRec.SubItems[iSubItemsCount], Length(ItemRec.SubItems[iSubItemsCount]));
end;
end;
finally
Free;
end;
end;
procedure LoadListViewItems(FileName: string; var ListView: TListView);
var
ItemRec: TItemRec;
iSubItemsCount: Integer;
Stream: TFileStream;
iNumSubItems: Integer;
begin
if not FileExists(FileName) then
Exit;
Stream := TFileStream.Create(FileName, fmOpenRead);
with Stream do
try
Position := 0;
while Position < Size do
begin
FillChar(ItemRec.Caption, Length(ItemRec.Caption), 0);
Read(ItemRec.Caption, Length(ItemRec.Caption));
Read(iNumSubItems, SizeOf(Integer));
with ListView.Items.Add do
begin
// legg caption til listviewen
Caption := ItemRec.Caption;
SetLength(ItemRec.SubItems, iNumSubItems);
for iSubItemsCount := 0 to iNumSubItems - 1 do
begin
FillChar(ItemRec.SubItems[iSubItemsCount], Length(ItemRec.SubItems[iSubItemsCount]), 0);
Read(ItemRec.SubItems[iSubItemsCount], Length(ItemRec.SubItems[iSubItemsCount]));
// legg sub item til listviewen
SubItems.Add(ItemRec.SubItems[iSubItemsCount]);
end;
end;
end;
finally
Free;
end;
end;
procedure TForm1.btnLoadClick(Sender: TObject);
begin
ListView1.Clear;
LoadListViewItems('listview.items', ListView1);
end;
procedure TForm1.btnSaveClick(Sender: TObject);
begin
SaveListViewItems('listview.items', ListView1);
end;
end.
-------------------------------------
I am AGAINST TCPA!!
[
http://www.againsttcpa.com/ ]
-------------------------------------
Mvh
DiZpel