Форум: "Основная";
Текущий архив: 2003.06.26;
Скачать: [xml.tar.bz2];
ВнизTListView Найти похожие ветки
← →
Mortal (2003-06-12 13:33) [0]Уважаемые мастера, может кто-нибудь привести пример сохранения и загрузки данных компонента ListView из фаила.
Заранее спасибо.
← →
Spawn (2003-06-12 13:48) [1]Например так:
procedure Save;
var
I,J:integer;
List:TStringList;
begin
List:=TStringList.Create;
try
for i:=0 to ListView1.Items.Count-1 do
begin
List.Add(ListView1.Items[i].Caption);
List.Add("@"+IntToStr(ListView1.Items[i].ImageIndex));
for j:=0 to ListView1.Items[i].SubItems.Count-1 do
List.Add(#9+ListView1.Items[i].SubItems[j];
end;
List.SaveToFile(ExtractFilePath(Application.ExeName)+"Items.txt");
finally
List.Free;
end;
end;
procedure Load;
var
List:TStringList;
NewItem:TListItem;
I:integer;
begin
NewItem:=nil;
ListView1.Items.Clear;
List:=TStringList.Create;
try
List.LoadFromFile(ExtractFilePath(Application.ExeName + "Items.txt");
for i:=0 to List.Count-1 do
if List[i][1]=#9 then
NewItem.SubItems.Add(Trim(List[i]))
else
if List[i][1]="@" then
NewItem.ImageIndex:=StrToIntDef(List[i][2],0);
else
begin
NewItem:=ListView1.Items.Add;
NewItem.Caption:=List[i];
end;
finally
List.Free;
end;
end;
← →
dataMaster (2003-06-12 16:30) [2]procedure LV_SaveToFile(LV: TListView; Signature, FileName: string);
var
idxItem, idxSub, IdxImage: Integer;
F: TFileStream;
pText: PChar;
sText: string;
W, ItemCount, SubCount: Word;
MySignature: array [0..2] of Char;
begin
with LV do
begin
ItemCount := 0;
SubCount := 0;
StrPCopy(MySignature, Signature);
F := TFileStream.Create(FileName, fmCreate or fmOpenWrite);
F.Write(MySignature, SizeOf(MySignature));
if Items.Count = 0 then
ItemCount := 0
else
ItemCount := Items.Count;
F.Write(ItemCount, SizeOf(ItemCount));
if Items.Count > 0 then
begin
for idxItem := 1 to ItemCount do
begin
with Items[idxItem - 1] do
begin
if SubItems.Count = 0 then
SubCount := 0
else
SubCount := Subitems.Count;
F.Write(SubCount, SizeOf(SubCount));
IdxImage := ImageIndex;
F.Write(IdxImage, SizeOf(IdxImage));
sText := Caption;
w := Length(sText);
pText := StrAlloc(Length(sText) + 1);
StrPLCopy(pText, sText, Length(sText));
F.Write(w, SizeOf(w));
F.Write(pText^, w);
StrDispose(pText);
if SubCount > 0 then
begin
for idxSub := 0 to SubItems.Count - 1 do
begin
sText := SubItems[idxSub];
w := Length(sText);
pText := StrAlloc(Length(sText) + 1);
StrPLCopy(pText, sText, Length(sText));
F.Write(w, SizeOf(w));
F.Write(pText^, w);
StrDispose(pText);
end;
end;
end;
end;
end;
F.Free;
end;
end;
procedure LV_LoadFromFile(LV: TListView; Signature, FileName: string);
var
F: TFileStream;
IdxItem, IdxSubItem, IdxImage: Integer;
W, ItemCount, SubCount: Word;
pText: PChar;
PTemp: PChar;
MySignature: array [0..2] of Char;
sExeName: string;
begin
with LV do
begin
ItemCount := 0;
SubCount := 0;
sExeName := ExtractFileName(FileName);
if not FileExists(FileName) then
Exit;
F := TFileStream.Create(FileName, fmOpenRead);
F.Read(MySignature, SizeOf(MySignature));
if MySignature <> Signature then
Exit;
F.Read(ItemCount, SizeOf(ItemCount));
Items.Clear;
for idxItem := 1 to ItemCount do
begin
with Items.Add do
begin
F.Read(SubCount, SizeOf(SubCount));
F.Read(IdxImage, SizeOf(IdxImage));
ImageIndex := IdxImage;
F.Read(w, SizeOf(w));
pText := StrAlloc(w + 1);
pTemp := StrAlloc(w + 1);
F.Read(pTemp^, W);
StrLCopy(pText, pTemp, W);
Caption := StrPas(pText);
StrDispose(pTemp);
StrDispose(pText);
if SubCount > 0 then
begin
for idxSubItem := 1 to SubCount do
begin
F.Read(w, SizeOf(w));
pText := StrAlloc(w + 1);
pTemp := StrAlloc(w + 1);
F.Read(pTemp^, W);
StrLCopy(pText, pTemp, W);
Items[idxItem - 1].SubItems.Add(StrPas(pText));
StrDispose(pTemp);
StrDispose(pText);
end;
end;
end;
end;
F.Free;
end;
end;
Страницы: 1 вся ветка
Форум: "Основная";
Текущий архив: 2003.06.26;
Скачать: [xml.tar.bz2];
Память: 0.46 MB
Время: 0.03 c