Question:
How do I save data entered in a list box at run time without resorting to a text file or having to deal with the overhead of a table? I am using 16bit Delphi 1.
Answer:
The easiest way to approach this is to use a TFileStream and TStream's ReadComponent and WriteComponent methods to open and save your saved list box. For instance, this is something that I use in my own code:
procedure TConfigForm.StreamList(Read : Boolean);
var
strm : TFileStream;
begin
//if Read then read the file off disk and load
//the list box. Otherwise, write it to disk.
//Notice that the Write methodology opens the
//stream with fmCreate. This will cause the
//current file to be overwritten
if Read then
begin
strm := TFileStream.Create(
ExtractFilePath(ParamStr(0) +
'ConfigList.DAT',
fmOpenRead);
try
strm.ReadComponent(lstConfigMain)
finally
strm.Free;
end;
end
else
begin
strm := TFileStream.Create(
ExtractFilePath(ParamStr(0) +
'ConfigLst.DAT',
fmCreate);
try
strm.WriteComponent(lstConfigMain)
finally
strm.Free;
end;
end;
end;
It's quick and dirty and demonstrates how to implement persistence in your components from run to run of your program.