Solved

Read/Save contents

Posted on 2001-09-14
2
219 Views
Last Modified: 2010-04-06
How can I use TStream to save and read the contents of 4 listboxs and 2 memos to "Demo.dat" file? Something like database. Exmaples please.
0
Comment
Question by:TCSCode
2 Comments
 
LVL 2

Expert Comment

by:bugroger
ID: 6483809
Hi,

Have a look at these two functions:

{
 SaveIT(ListBox1, ListBox2, ListBox3, ListBox4, Memo1, Memo2, 'C:\Demo.dat');
LoadIt(ListBox1, ListBox2, ListBox3, ListBox4, Memo1, Memo2, 'C:\Demo.dat');
}

Procedure SaveIT(LB1, LB2, LB3, LB4 : TListBox; M1, M2 : TMemo; FileName : String);
VAR
 FS           : TFileStream;
 DummyStream  : TMemoryStream;
 Size         : Integer;

Begin
 FS          := TFileStream.Create(FileName, fmCreate or fmSHAREDENYNONE);
 DummyStream := TMemoryStream.Create;

 LB1.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 LB2.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 LB3.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 LB4.Items.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 M1.Lines.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 M2.Lines.SaveToStream(DummyStream);
 Size := DummyStream.Size;
 FS.Write(Size, 4);
 FS.Write(DummyStream.Memory^, Size);

 DummyStream.Free;
 FS.Free;
End;

Procedure LoadIT(LB1, LB2, LB3, LB4 : TListBox; M1, M2 : TMemo; FileName : String);
VAR
 FS           : TFileStream;
 DummyStream  : TMemoryStream;
 Size         : Integer;

Begin
 FS          := TFileStream.Create(FileName, fmOpenRead or fmSHAREDENYNONE);
 DummyStream := TMemoryStream.Create;

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB1.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB2.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB3.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 LB4.Items.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 M1.Lines.LoadFromStream(DummyStream);

 FS.Read(Size, 4);
 DummyStream.Size := Size;
 FS.Read(DummyStream.Memory^, Size);
 M2.Lines.LoadFromStream(DummyStream);

 DummyStream.Free;
 FS.Free;
End;
0
 

Accepted Solution

by:
duti earned 300 total points
ID: 6484085
const
  sFileName ='demo.dat';
{---------------------------------------------------------------------------
  TForm1.Button1Click
---------------------------------------------------------------------------}
procedure TForm1.Button1Click(Sender: TObject);
Begin
  // you can save as much listboxes/memos as you want,
  // without additional coding in the save/load routines.
  SaveData( [ ListBox1, Memo1, ListBox2, listbox3, Memo2 ] );
end;

{---------------------------------------------------------------------------
  TForm1.Button2Click
---------------------------------------------------------------------------}
procedure TForm1.Button2Click(Sender: TObject);
begin
  LoadData; // everything works automagically
end;

{---------------------------------------------------------------------------
  TForm1.SaveData
---------------------------------------------------------------------------}
procedure TForm1.SaveData(aComp : array of TComponent);
var
  stream : TFileStream;
  i      : integer;

  procedure _Save(const sName, sData : string);
  var iLength : integer;
  begin
    iLength := length(sName);
    stream.Write(iLength, sizeof(iLength));
    stream.Write(pchar(sName)^, length(sName));

    iLength := length(sData);
    stream.Write(iLength, sizeof(iLength));
    stream.Write(pchar(sData)^, length(sData));
  end;
begin
  stream := TFileStream.Create(sFileName, fmCreate or fmShareDenyNone);
  try
    for i := low(aComp) to high(aComp) do
      if aComp[i] is TListbox then _Save(aComp[i].Name, TListbox(aComp[i]).Items.Text)
      else if aComp[i] is TMemo then _Save(aComp[i].Name, TMemo(aComp[i]).Lines.Text);
  finally
    stream.Free;
  end;
end;

{---------------------------------------------------------------------------
  TForm1.LoadData
---------------------------------------------------------------------------}
procedure TForm1.LoadData;
var
  stream       : TFileStream;
  iLength      : integer;
  iBytesRead   : integer;
  sName        : string;
  sText        : string;
  Comp         : TComponent;
begin
  stream := TFileStream.Create(sFileName, fmOpenRead or fmShareDenyNone);
  try
    repeat
      iBytesRead := stream.Read(iLength, SizeOf(iLength));
      if iBytesRead > 0 then
        begin
          SetLength(sName, iLength);
          stream.Read(pchar(sName)^, iLength);

          stream.Read(iLength, SizeOf(iLength));
          SetLength(sText, iLength);
          stream.Read(pchar(sText)^, iLength);
          Comp := FindComponent(sName);
          if Comp <> nil then
            begin
              if Comp is TListBox then TListBox(Comp).Items.Text := sText
              else if Comp is TMemo then TMemo(Comp).Lines.Text := sText;
            end;
        end;
    until iBytesRead = 0;
  finally
    stream.Free;
  end;
end;
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now