View Single Post
  #6  
Old 08-25-2005, 05:19 PM
BaraoZemo BaraoZemo is offline
Senior Member
 
Join Date: Nov 2001
Posts: 3,598
Default RE: listview items save to file

this code works very accurated for me..

and works with all kind of components ..(except containers such as TPanel, TForm, for these components , before reload you need first to clear the items inside to avoid error)

//to save
SaveFile(ChangeFileExt(ParamStr(0), '.sav'), listview1);

//to load
LoadFile(ChangeFileExt(ParamStr(0), '.sav'), listview1);

Code:
procedure SaveFile(filename: String; a: TComponent);
var
 FileStream:TFileStream;
 FileWriter:TWriter;
 BufferSize:Integer;
begin
 try
   BufferSize := 1024;
   FileStream := TFileStream.Create(filename,fmOpenWrite or fmCreate);
   FileWriter := TWriter.Create(FileStream,BufferSize);
   FileWriter.WriteRootComponent(a);
 finally
   FileWriter.Free;
   FileStream.Free;
 end;
end;

procedure LoadFile(filename: string; a: TComponent);
var
 FileStream:TFileStream;
 FileReader:TReader;
 BufferSize:Integer;
 Identification:String;
begin
 try
   BufferSize := 1024;
   FileStream := TFileStream.Create(filename,fmOpenRead);
   FileReader := TReader.Create(FileStream,BufferSize);
   FileReader.ReadRootComponent(a);
 finally
   FileReader.Free;
   FileStream.Free;
 end;
end;
[link=http://www.delphipages.com/threads/thread.cfm?ID=27230&G=27222]original post is here[/link]

Regards
BaraoZemo

ps, donīt forget to click in the button and Accept as Answer if this helped you!
Reply With Quote