Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to save components to a file or stream (3) Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
20-Oct-02
Category
Files Operation
Language
Delphi 6.x
Views
89
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Jonas Bilinkevicius

In D6, what's the best or usual way to stream an object to text so that it shows up 
just as it does when you copy an object to the clipboard or when you display a form 
as text?

Answer:

This example shows how to use the built-in VCL component streaming support to 
convert any component into a string and convert that string back into a component.
1   
2   function ComponentToString(Component: TComponent): string;
3   var
4     BinStream: TMemoryStream;
5     StrStream: TStringStream;
6     s: string;
7   begin
8     BinStream := TMemoryStream.Create;
9     try
10      StrStream := TStringStream.Create(s);
11      try
12        BinStream.WriteComponent(Component);
13        BinStream.Seek(0, soFromBeginning);
14        ObjectBinaryToText(BinStream, StrStream);
15        StrStream.Seek(0, soFromBeginning);
16        Result := StrStream.DataString;
17      finally
18        StrStream.Free;
19      end;
20    finally
21      BinStream.Free
22    end;
23  end;
24  
25  function StringToComponent(Value: string): TComponent;
26  var
27    StrStream: TStringStream;
28    BinStream: TMemoryStream;
29  begin
30    StrStream := TStringStream.Create(Value);
31    try
32      BinStream := TMemoryStream.Create;
33      try
34        ObjectTextToBinary(StrStream, BinStream);
35        BinStream.Seek(0, soFromBeginning);
36        Result := BinStream.ReadComponent(nil);
37      finally
38        BinStream.Free;
39      end;
40    finally
41      StrStream.Free;
42    end;
43  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC