RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TStrings.SaveToStream Method (TStream, TEncoding)

Writes the value of the Text property to a stream object.

Pascal
procedure SaveToStream(Stream: TStream; Encoding: TEncoding); virtual; overload;
C++
virtual __fastcall SaveToStream(TStream Stream, TEncoding Encoding);

Call SaveToStream@TStream@TEncoding to save the strings in the list to the stream specified by the Stream parameter. SaveToStream writes the strings delimited by carriage return, line feed pairs. If the stream is a file stream, SaveToStream does the same thing as SaveToFile, except the application must create and destroy the file stream. 

If the Encoding parameter is not given, then the strings are saved with the default encoding, as specified in the Default property of the TEncoding class.  

C++ Examples:  

/*
This example requires TListBox, TMemo, TRichEdit, and
TButton controls placed on the form. The list box should
contain one or more items.  When the form becomes visible,
click on the button and the contents of the list box will be
transferred to a stream and then to the rich edit control.
*/
#include <memory>       //for STL auto_ptr class
        
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    std::auto_ptr<TMemoryStream> pms(new TMemoryStream());
    ListBox1->Items->SaveToStream(pms.get()); // write list box contents to the
                            // stream.
    pms->Position = 0;                           // reset to the beginning of the stream.
    RichEdit1->Lines->LoadFromStream(pms.get()); // load stream contents into rich
                            // edit control.
}

C++ Examples:

{
This example requires TListBox, TMemo, TRichEdit, and
TButton controls placed on the form. The list box should
contain one or more items.  When the form becomes visible,
click on the button and the contents of the list box will be
transferred to a stream and then to the rich edit control.
}
procedure TForm1.Button1Click(Sender: TObject);
var
    TempStream : TMemoryStream;
begin
    TempStream := TMemoryStream.Create;
    ListBox1.Items.SaveToStream(TempStream);  // write list box contents to the
                            // stream
    TempStream.Position := 0;      // reset to the beginning of the stream
    RichEdit1.Lines.LoadFromStream( TempStream); // load stream contents into rich 
                            // edit control
    TempStream.Free;
end;
    
procedure TForm1.FormCreate(Sender: TObject);
begin
    ListBox1.Items.Add('Apples');
    ListBox1.Items.Add('Oranges');
    ListBox1.Items.Add('Pears');
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!