Plain text, apply style programmatically

General TRichView support forum. Please post your questions here
Post Reply
agent86
Posts: 59
Joined: Mon Jun 28, 2010 2:18 am
Location: San Francisco Bay Area, USA
Contact:

Plain text, apply style programmatically

Post by agent86 »

RichView 13

I read plain text from a database to the DBRichViewEdit. It comes in as Arial font size 10. I would like it to take on the font and font size in the combo boxes. The default is Trebuchet, font size 10.

Also if I select the plain text and click on font name combobox the text selected in the DBRichView loses focus.
agent86
Posts: 59
Joined: Mon Jun 28, 2010 2:18 am
Location: San Francisco Bay Area, USA
Contact:

Is there a solution to this problem?

Post by agent86 »

See previos post...

I am still waiting for a solution if there is one. If not please let me know.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

1) To define the default font, use OnNewDocument event.
In this event, you can use this code (assuming that your DBRichViewEdit is linked to RVStyle1):

Code: Select all

RVStyle1.TextStyles.Clear;
RVStyle1.TextStyles.Add;
RVStyle1.TextStyles[0].FontName := 'Trebuchet';
RVStyle1.TextStyles[0].Size := 10;
RVStyle1.ParaStyles.Clear;
RVStyle1.TParaStyles.Add;
RVStyle1.ListStyles.Clear;
2) A combobox control takes focus. Add DBRichViewEdit.SetFocus in your code that applies changes made in this combobox.
agent86
Posts: 59
Joined: Mon Jun 28, 2010 2:18 am
Location: San Francisco Bay Area, USA
Contact:

Thank you...

Post by agent86 »

I'll give this a try. Thank you...
agent86
Posts: 59
Joined: Mon Jun 28, 2010 2:18 am
Location: San Francisco Bay Area, USA
Contact:

I've done something wrong or did not understand...

Post by agent86 »

This is my procedure. Did not work. I just used a single word for the test...

Code: Select all

procedure TReportWriterMainForm.ConvertPlainTextToRTFRVE ;
var s, pt: TRVAnsiString;
    Stream: TMemoryStream;
    fs : string  ; // font size
begin
 // dbrichview
 rve.clear ;

 pt := 'Booga' ;

  Stream := TMemoryStream.Create;
  Stream.WriteBuffer(PRVAnsiChar(s)^, Length(s));
  Stream.Position := 0;
  rve.InsertRTFFromStreamEd(Stream);
  Stream.Free;

  rvs.TextStyles.Clear;
  rvs.TextStyles.Add;
  rvs.TextStyles[0].FontName := 'Trebuchet MS';
  rvs.TextStyles[0].Size := 10;
  rvs.ParaStyles.Clear;
  rvs.ParaStyles.Add;
  rvs.ListStyles.Clear;

end;
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Please describe what exactly you want.

I assumed that you stored a plain text in a database, and you want to define a font for this plain text in a linked TDBRichViewEdit.

I do not understand your code. What's the source of data? You write s to the stream, but s is not initialized. What's the source data format? What do you want in results?
agent86
Posts: 59
Joined: Mon Jun 28, 2010 2:18 am
Location: San Francisco Bay Area, USA
Contact:

Sorry. Error in code...

Post by agent86 »

I figured I goofed sometyhing up and I did!

I had changed the variable to pt instead of s and forgot to change the s to pt. During my test phase I am using a string, pt, instead of reading from a database. The database will store plain text that must be formatted when I write to the dbrichview.

The following code now "works" but the font size comes in as 12 instead of 10. Otherwise it seems to work.

Code: Select all

procedure TReportWriterMainForm.ConvertPlainTextToRTFRVE ;
var pt: TRVAnsiString;
    Stream: TMemoryStream;

begin
  // dbrichview
  rve.clear ;

  rvs.TextStyles.Clear;
  rvs.TextStyles.Add;
  rvs.TextStyles[0].FontName := 'Trebuchet MS';
  rvs.TextStyles[0].Size := 10;
  rvs.ParaStyles.Clear;
  rvs.ParaStyles.Add;
  rvs.ListStyles.Clear;

  // normally the text will come from a database field
  pt := 'This is a test!' ;

  Stream := TMemoryStream.Create;
  Stream.WriteBuffer(PRVAnsiChar(pt)^, Length(pt));
  Stream.Position := 0;
  rve.InsertRTFFromStreamEd(Stream);
  Stream.Free;


end;
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

If you want to add a plain text, just write

Code: Select all

  rve.clear ; 

  rvs.TextStyles.Clear; 
  rvs.TextStyles.Add; 
  rvs.TextStyles[0].FontName := 'Trebuchet MS'; 
  rvs.TextStyles[0].Size := 10; 
  rvs.ParaStyles.Clear; 
  rvs.ParaStyles.Add; 
  rvs.ListStyles.Clear; 

  // normally the text will come from a database field 
  pt := 'This is a test!' ; 
  rve.AddTextNL(pt, 0, 0, 0);
  rve.Format;
Or, in a real application, pt will contain a real RTF that you need to display as a plain text?
agent86
Posts: 59
Joined: Mon Jun 28, 2010 2:18 am
Location: San Francisco Bay Area, USA
Contact:

Why is my font size 12 when we are telling it 10?

Post by agent86 »

Do you see the reason I'm getting Font Size of 12 in my Font Size Combo Box instead of 10 as is designated in the procedure above?
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

In your procedure? In new version of TRichView, font name and size are taken from RTF. Since TRichView 13, if fonts are not specified in RTF, default RTF font is applied.
Post Reply