Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Pascal
Expertise: Beginner
Nov 4, 1998

Saving List Box Data at Run Time 16 Bit Delphi 1

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.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap