Link to home
Start Free TrialLog in
Avatar of voodoo22
voodoo22

asked on

question about textRTF property

This question is for anyone that is familiar with both delphi and vb.

I am trying to port a vb app over to delphi and in the source code for the vb app they use the textRTF property of a RichTextBox control and not being a vb guru I went to MSDN and looked it up and it gave me this explanation of what it was.

"Returns or sets the text of a RichTextBox control, including all .rtf code."

which did not really help me alot so I thought I would come here and ask the experts for more info  and more importanly if there is a Delphi equivalent for the textRTF property.

btw if anyone is interested this is the msdn page I looked at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rtfbox98/html/vbprotextrtf.asp

Pete
Avatar of shaneholmes
shaneholmes

If you're using the Visual Basic rich text control it has a
.Text property and a .TextRTF property.  The .TextRTF property will return
the text and all the associated format codes, which you can read in with your TRichEdit in delphi.


Shane
Sorry, Then you read in using the TRichEdit.lines.LoadFromFIle

Shane
Avatar of Russell Libby
You can use the Line.LoadFromFile or the line.LoadFromStream. If you have rtf formatted text, you can save this to a stream (TMemoryStream, TStringStream) then have the RIchEdit control load it by using Lines.LoadFromStream(StreamObject).

Regards,
Russell


eg:

var  Strm:       TMemoryStream;
begin

  RichEdit1.Lines.LoadFromFile('c:\delphi5\demos\richedit\overview.rtf');
  Strm:=TMemoryStream.Create;
  RichEdit1.Lines.SaveToStream(Strm);
  ShowMessage(PChar(Strm.Memory));

  Strm.Position:=0;
  RichEdit1.Lines.LoadFromStream(Strm);

end;
Avatar of voodoo22

ASKER

let me see if i get this right. Using the text property would return the text as it was and using the textRTF would return the RTF Table for the text ???


textRTF would return plain text with format codes

Shane
.text, would return plain text without any formatting

Shane
ok then obviously my next question is what do you mean by format codes the fsBold , fsUnderline stuff or something else ... could you provide an example of what is returned so I can see what I can expect. I don't need any code just what it would look like.
SOLUTION
Avatar of shaneholmes
shaneholmes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There you go - Russell  Gave you everything you need!

<SMILE>

Shane
Thanks for the comments but now I am more lost then when I first asked the question and although the comments have not helped me understand this any better I have to award the point so I will just split them up between you guys.
voodoo22,

We would have helped you to understand better. Do you still want help?

Shane
I do need help thats true but I did not want to waste any more of your guys time with a simple 100 point question but if your willing to then the problem I am having is I don't know how you got all that formatting from the richedit control because when I saved the contents of the control to a text file this is all that showed up.

{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}
\viewkind4\uc1\pard\b\f0\fs24 Hello\b0 , My \i Name\i0  is \ul Shane\ulnone !
\par }

and when I reloaded it up into the richedit control it showed up exactly the same as yours did so again I have no idea how you got more then I did.

And as far as rlibbys comment / code goes I have no clue how use it. I am just hobbiest programmer (if you can call me that) and not been doing this for very long and I have never had any training on programmig of any kind and I simply don't know about classes and casting and all that stuff.

voodoo22....

First, if you didn't understand, you should have asked further questions; not assigned a B grade for this question.

The meaning of TextRTF is the tokenized text that makes up an RTF file. The rich text format (RTF) IS string data, but contains tokens
that must be converted to the appropriate formatting type (such as language, paragraphs, colors, fonts, etc..). As Shane
desribed to you, an example of TextRTF text might look like the following:

<start>
{\rtf1\ansi\ansicpg1252\uc1\deff0\stshfdbch0\stshfloch0\stshfhich0\stshfbi0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f36\froman\fcharset238\fprq2 Times New Roman CE;}
{\f37\froman\fcharset204\fprq2 Times New Roman Cyr;}{\f39\froman\fcharset161\fprq2 Times New Roman Greek;}{\f40\froman\fcharset162\fprq2 Times New Roman Tur;}{\f41\froman\fcharset177\fprq2 Times New Roman (Hebrew);}
<etc...>

This "string" data is run through a converter to apply the correct code page, font, color, paragraph, etc formatting
in the edit window when displayed.

The "Text" property of the RTF file is the raw text that is displayed. Think of it as the output text, minus the paragraph/color/font/etc formatting that is
applied to it when converted from RTF.

----

Russell


OK,

First, From Delphi, Create a New Component by doing the folowing: (From the main menu)

File --> New --> Other

From the "New" tab, double click on "Component" (to create a new component).

A New Component Dialog Box Appears:

Select from the Ancestor Type drop down, TRichEdit (or type it in.

In my Delphi 7, it is listed as "TRichEdit  [ComCtrls]"

Change Class name to :  "TEXtRichEdit1" (no quotes of course)

Click the install button

You wan to install into new package, so select the new package tab
 Browse to your Delphi\Lib folder

Misc

and click the open Button

then OK

then "yes" to Build


Now, in the Unit you have there  unit ExtRichEdit1;

Fill in all the rest of the code as Russell laid out for you above.


You can cust & paste alot of it.

The Compile and you are ready to go, you have a RichEdit for Delphi comparable to your VB one.

Let us know if you get stuck

Shane

Shane






OK, Now when you have the new TRichEdit (TExtRichEdit)  on your Component Pallete, you can use it as Russell laid out:

Shane

And example usage
--------------------------

begin

  ExtRichEdit1.Lines.LoadFromFile('c:\delphi5\demos\richedit\overview.rtf');

  with TExtRichEditCast.Create(RichEdit1) do
  begin
     ShowMessage(TextRTF);
     ShowMessage(Text);
     Free;
  end;

end;
Ok I did all that and it all seemed to go smoothly until it was time to compile the unit which turned up the following errors.

[Error] EXtRichEdit1.pas(10): ',' or ':' expected but '=' found
[Error] EXtRichEdit1.pas(35): 'END' expected but 'IMPLEMENTATION' found
[Error] EXtRichEdit1.pas(37): Undeclared identifier: 'TRichEditCast'
[Error] EXtRichEdit1.pas(41): This form of method call only allowed in methods of derived types
[Error] EXtRichEdit1.pas(44): Undeclared identifier: 'FSelf'
[Error] EXtRichEdit1.pas(48): Declaration of 'TRichEditCast' differs from previous declaration
[Error] EXtRichEdit1.pas(52): This form of method call only allowed in methods of derived types
[Error] EXtRichEdit1.pas(56): Identifier redeclared: 'TRichEditCast'
[Error] EXtRichEdit1.pas(59): Undeclared identifier: 'FPlainText'
[Error] EXtRichEdit1.pas(59): Undeclared identifier: 'FSelf'
[Error] EXtRichEdit1.pas(63): Identifier redeclared: 'TRichEditCast'
[Error] EXtRichEdit1.pas(66): Undeclared identifier: 'FSelf'
[Error] EXtRichEdit1.pas(66): Undeclared identifier: 'FPlainText'
[Error] EXtRichEdit1.pas(70): Identifier redeclared: 'TRichEditCast'
[Error] EXtRichEdit1.pas(74): Undeclared identifier: 'SaveState'
[Error] EXtRichEdit1.pas(76): Undeclared identifier: 'FSelf'
[Error] EXtRichEdit1.pas(77): Undeclared identifier: 'Value'
[Error] EXtRichEdit1.pas(79): Missing operator or semicolon
[Error] EXtRichEdit1.pas(84): Undeclared identifier: 'RestoreState'
[Error] EXtRichEdit1.pas(89): Identifier redeclared: 'TRichEditCast'
[Error] EXtRichEdit1.pas(93): Undeclared identifier: 'SaveState'
[Error] EXtRichEdit1.pas(95): Undeclared identifier: 'FSelf'
[Error] EXtRichEdit1.pas(96): Undeclared identifier: 'Value'
[Error] EXtRichEdit1.pas(98): Missing operator or semicolon
[Error] EXtRichEdit1.pas(103): Undeclared identifier: 'RestoreState'
[Error] EXtRichEdit1.pas(108): Function needs result type
[Error] EXtRichEdit1.pas(112): Undeclared identifier: 'SaveState'
[Error] EXtRichEdit1.pas(114): Undeclared identifier: 'FSelf'
[Error] EXtRichEdit1.pas(117): Missing operator or semicolon
[Error] EXtRichEdit1.pas(123): Undeclared identifier: 'RestoreState'
[Error] EXtRichEdit1.pas(128): Function needs result type
[Error] EXtRichEdit1.pas(132): Undeclared identifier: 'SaveState'
[Error] EXtRichEdit1.pas(134): Undeclared identifier: 'FSelf'
[Error] EXtRichEdit1.pas(137): Missing operator or semicolon
[Error] EXtRichEdit1.pas(143): Undeclared identifier: 'RestoreState'
[Hint] EXtRichEdit1.pas(13): Private symbol 'FSelf' declared but never used
[Hint] EXtRichEdit1.pas(14): Private symbol 'FPlainText' declared but never used
[Error] EXtRichEdit1.pas(17): Unsatisfied forward or external declaration: '.2.SaveState'
[Error] EXtRichEdit1.pas(18): Unsatisfied forward or external declaration: '.2.RestoreState'
[Error] EXtRichEdit1.pas(19): Unsatisfied forward or external declaration: '.2.SetText'
[Error] EXtRichEdit1.pas(20): Unsatisfied forward or external declaration: '.2.SetTextRTF'
[Error] EXtRichEdit1.pas(21): Unsatisfied forward or external declaration: '.2.GetText'
[Error] EXtRichEdit1.pas(22): Unsatisfied forward or external declaration: '.2.GetTextRTF'
[Error] EXtRichEdit1.pas(25): Unsatisfied forward or external declaration: '.2.Create'
[Error] EXtRichEdit1.pas(26): Unsatisfied forward or external declaration: '.2.Destroy'
[Error] EXtRichEdit1.pas(33): Unsatisfied forward or external declaration: 'TEXtRichEdit1.Register'
[Fatal Error] Misc.dpk(33): Could not compile used unit 'EXtRichEdit1.pas'


and here is the contents of the unit.....................


unit EXtRichEdit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, StdCtrls, ComCtrls;

type
  TEXtRichEdit1 = class(TRichEdit)
  TRichEditCast  =  class(TObject)
  private
    { Private declarations }
    FSelf:      TRichEdit;
    FPlainText: Boolean;
  protected
    { Protected declarations }
     procedure   SaveState;
     procedure   RestoreState;
     procedure   SetText(Value: String);
     procedure   SetTextRTF(Value: String);
     function    GetText: String;
     function    GetTextRTF: String;
  public
    { Public declarations }
     constructor Create(RichEditCtl: TRichEdit);
     destructor  Destroy; override;
     property    Text: String read GetText write SetText;
     property    TextRTF: String read GetTextRTF write SetTextRTF;
  published
    { Published declarations }
  end;

procedure Register;

implementation

constructor TRichEditCast.Create(RichEditCtl: TRichEdit);
begin

  // Perform inherited
  inherited Create;

  // Set reference
  FSelf:=RichEditCtl;

end;

destructor TRichEditCast.Destroy;
begin

  // Perform inherited
  inherited Destroy;

end;

procedure TRichEditCast.SaveState;
begin

  FPlainText:=FSelf.PlainText;

end;

procedure TRichEditCast.RestoreState;
begin

  FSelf.PlainText:=FPlainText;

end;

procedure TRichEditCast.SetText(Value: String);
var  stream:     TStringStream;
begin

  SaveState;
  try
     FSelf.PlainText:=True;
     stream:=TStringStream.Create(Value);
     try
        FSelf.Lines.LoadFromStream(stream);
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;

procedure TRichEditCast.SetTextRTF(Value: String);
var  stream:     TStringStream;
begin

  SaveState;
  try
     FSelf.PlainText:=False;
     stream:=TStringStream.Create(Value);
     try
        FSelf.Lines.LoadFromStream(stream);
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;

function TRichEditCast.GetText: String;
var  stream:     TStringStream;
begin

  SaveState;
  try
     FSelf.PlainText:=True;
     stream:=TStringStream.Create('');
     try
        FSelf.Lines.SaveToStream(stream);
        result:=stream.DataString;
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;

function TRichEditCast.GetTextRTF: String;
var  stream:     TStringStream;
begin

  SaveState;
  try
     FSelf.PlainText:=False;
     stream:=TStringStream.Create('');
     try
        FSelf.Lines.SaveToStream(stream);
        result:=stream.DataString;
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;


procedure Register;
begin
  RegisterComponents('samples', [TEXtRichEdit1]);
end;

end.

First, the "wrapper" i gave you was so you didn't have to create a new component (as you see above, it inherits from TObject),
It is not an OO extension to the original, although it could be used that way. If you wish to use it as an OO component extension, then
the correct declarations would be as follows:

unit ExtRichEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TExtRichEdit      = class(TRichEdit)
  private
     // Private declarations
     FPlainText:    Boolean;
  protected
     // Protected declarations
     procedure   SaveState;
     procedure   RestoreState;
     procedure   SetText(Value: String);
     procedure   SetTextRTF(Value: String);
     function    GetText: String;
     function    GetTextRTF: String;
  public
     // Public declarations
  published
     // Published declarations
     property    Text: String read GetText write SetText;
     property    TextRTF: String read GetTextRTF write SetTextRTF;
  end;

procedure Register;

implementation

procedure TExtRichEdit.SaveState;
begin

  FPlainText:=PlainText;

end;

procedure TExtRichEdit.RestoreState;
begin

  PlainText:=FPlainText;

end;

procedure TExtRichEdit.SetText(Value: String);
var  stream:     TStringStream;
begin

  SaveState;
  try
     PlainText:=True;
     stream:=TStringStream.Create(Value);
     try
        Lines.LoadFromStream(stream);
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;

procedure TExtRichEdit.SetTextRTF(Value: String);
var  stream:     TStringStream;
begin

  SaveState;
  try
     PlainText:=False;
     stream:=TStringStream.Create(Value);
     try
        Lines.LoadFromStream(stream);
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;

function TExtRichEdit.GetText: String;
var  stream:     TStringStream;
begin

  SaveState;
  try
     PlainText:=True;
     stream:=TStringStream.Create('');
     try
        Lines.SaveToStream(stream);
        result:=stream.DataString;
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;

function TExtRichEdit.GetTextRTF: String;
var  stream:     TStringStream;
begin

  SaveState;
  try
     PlainText:=False;
     stream:=TStringStream.Create('');
     try
        Lines.SaveToStream(stream);
        result:=stream.DataString;
     finally
        stream.Free;
     end;
  finally
     RestoreState;
  end;

end;

procedure Register;
begin
  RegisterComponents('Win32', [TExtRichEdit]);
end;

end.

-----------

Menu | component/install component/dialog button Browse (for Unit file name)/ Select ExtRichEdit.pas (from where ever you saved it)
Compile the packge;  if the dialog does not indicate that TExtRichEdit was installed, then click the "Install" button for the package dialog.
Once installed, you will find the TExtRichEdit component on the Win32 tab of the ide. This can be dropped on the form (instead of TRichEdit).
The component will have all of the TRichEdit properites, as well as the Text and TextRTF properties exposed by the descendant class.


Russell




Ok I installed the component and ran this code on a button

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExtRichEdit1.Lines.LoadFromFile('goof.txt');
  ShowMessage(ExtRichEdit1.TextRTF);
  ShowMessage(ExtRichEdit1.Text);
end;

goof.txt contained all those RTF codes that shane posted above but when I clicked on the button all that showed in the message box was

{\rtf1\ansi\ansicpg1252\deff0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}}
\viewkind4\uc1\pard\b\f0\fs24 Hello\b0 , My \i Name\i0  is \ul Shane\ulnone !
\par }

which is obviously not the same thing as what he has above but oddly enough it is the exact same thing I posted above
so which one is correct and don't worry I am going to open a new blank question and award you another 100 points once
I get this thing figured out.
Nvm I got the program working thx for your help again.