2009. május 29., péntek

How to copy RTF text to a TMemo via a TStringStream


Problem/Question/Abstract:

How to copy RTF text to a TMemo via a TStringStream

Answer:

Here is a method to copy the formatted text to a memo via a stringstream. The same approach should work to store it in a database field that takes a string.

procedure TForm1.Button1Click(Sender: TObject);
var
  ss: TStringStream;
begin
  ss := TStringStream.Create(EmptyStr);
  try
    richedit1.plaintext := false;
    richedit1.lines.savetostream(ss);
    memo1.text := ss.DataString;
  finally
    ss.free;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ss: TStringStream;
begin
  ss := TStringStream.Create(memo1.text);
  try
    richedit1.plaintext := false;
    richedit1.lines.LoadFromstream(ss);
  finally
    ss.free;
  end;
end;

To simply copy formatted text from a TRichEdit to a string, you could also use:

function GetRawRTFText(aRichedit: TRichedit): string;
var
  SS: TStringstream;
begin
  SS := TStringstream.Create(EmptyStr);
  try
    aRichedit.plaintext := False;
    arichedit.Lines.SaveToStream(SS);
    Result := SS.DataString;
  finally
    SS.Free
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése