Board index » delphi » TRichEdit : saving and getting back rich text from an untyped file

TRichEdit : saving and getting back rich text from an untyped file

Peter Below helped me learn how to use a MemoryStream to "extract" the rich
text, rather than just the "plain" text from a RichEdit control (November
29th).

I save the text along with other fields in a binary file.  When I read back
the file and get the richtext string how do I put that in the Rich Text box
and show the text with formatting rather than the whole richtext encoded
string?

Thanks for any help? (Meanwhile I'm trying this, trying that :) )

Sincerely

Paul
pwhol...@prodigy.net

 

Re:TRichEdit : saving and getting back rich text from an untyped file


 >Peter Below helped me learn how to use a MemoryStream to "extract" the
rich

Quote
>text, rather than just the "plain" text from a RichEdit control (November
>29th).

can you please point to the place to find this messege, november 29 is in
the future and in october 29 I did not find it,
I am looking for help on this subject. reading a RTF file into a richedit
contril.
Thanks
Sraya

Re:TRichEdit : saving and getting back rich text from an untyped file


Sraya:

Use Lines.LoadFromFile. Be sure that the PlainText property is set to false.

--
Regards
Ralph (TeamB)
(TeamB cannot respond to questions received via email)

Quote
Sraya Malkiel wrote in message <71umvq$h...@forums.borland.com>...

|I am looking for help on this subject. reading a RTF file into a richedit
|contril.

Re:TRichEdit : saving and getting back rich text from an untyped file


Sorry, Sraya, meant Oct 29th, Under the message, "Copy Lines in Rich
Edit-----" (See Peter Below's answer).

Re:TRichEdit : saving and getting back rich text from an untyped file


Quote
> I save the text along with other fields in a binary file.  When I read back
> the file and get the richtext string how do I put that in the Rich Text box
> and show the text with formatting rather than the whole richtext encoded
> string?

Paul,

since the richedit.lines.loadfromstream always reads to the end of stream you
will have to first extract the rich text part from your stored data, into an
auxillary memory stream. I hope you write the length of the data before you
store the rich text data or you will have trouble finding its end!

 Example (UNTESTED!):

 Procedure SaveRichEdit( re: Trichedit; toStream: TStream );
 Var
   ms: TMemoryStream;
   oldPlaintext: Boolean;
   numBytes: Integer;
 Begin
   ms:= TMemoryStream.Create;
   try
     oldPlainText := re.Plaintext;
     try
       re.Plaintext := False;
       re.lines.savetostream( ms );
     finally  
       re.Plaintext := oldPlaintext;
     end;
     ms.Position := 0;
     numBytes := ms.Size;
     toStream.WriteBuffer( numBytes, Sizeof( numBytes ));
     toStream.CopyFrom( ms, numbytes );
   finally
     ms.free
   end;
 End;  

 Procedure LoadRichEdit( re: Trichedit; fromStream: TStream );
 Var
   ms: TMemoryStream;
   oldPlaintext: Boolean;
   numBytes: Integer;
 Begin
   fromStream.ReadBuffer( numBytes, Sizeof( numBytes ));
   ms:= TMemoryStream.Create;
   try
     ms.CopyFrom( fromStream, numbytes );
     ms.Position := 0;
     oldPlainText := re.Plaintext;
     try
       re.Plaintext := False;
       re.lines.loadfromstream( ms );
     finally  
       re.Plaintext := oldPlaintext;
     end;
   finally
     ms.free
   end;
 End;  

Note that if you store mixed binary data into a stream your reading code must
exactly mirror the writing code, otherwise you will mix up the data something
fierce. If you call LoadrichEdit and the stream you pass it is not sitting on
the length of the data, followed by the rich text it will load garbage!
The length has to be accurate, the rich edit control is very fickle when it
comes to streaming its contents: you try to load data that does not start
with a '{\rtf1' and end with a '}', with the proper balance of braces in
between, and it will show the data as plain text!

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Re:TRichEdit : saving and getting back rich text from an untyped file


Thanks, Peter.

Actually, your method for saving the RichText encoded string to a Memory
Stream (Nov 29th -- under "Copy Lines in RichEdit), allowed me to then save
the string to a binary file which I already had coded to return the string
properly into an object I had created.

Then I finally figured out the following method for loading that string from
the object I placed it in:

procedure RTFStringToRTFBox(RTFStr:String;var RTFBox:TRichedit);
{This function takes an RTF-coded string and puts it in a
RichText box whose PlainText value is set to false}
var
 ms:TMemoryStream;
begin
  try
      ms:= TMemorySTream.Create;
      ms.Write(PChar(RTFstr)^,Length(RTFStr));
      ms.Position := 0;
      RTFBox.Lines.LoadFromStream(ms);
  finally
     ms.Free;
  end;//end of try-finally-end
end;

Thanks for your help, though.  It was invaluable for proceeding since I'm in
the process of re-using Delphi after a year and a half of only Visual Basic.
by the way, Delphi Rules!  (Or at least it should!)

Paul
pwhol...@prodigy.net

Other Threads