Re: set TRichEdit text in bold

Posted by webmaster Guido on June 04, 2005

In Reply to: Bold Text TRichEdit posted by joe90 on June 02, 2005

: I'm wanting to add some text to a TRichEdit, but I want some of it written in a bold font style. I know I can TStrings of the Lines property - but I want to add some of it in bold. Such as:

: Title of paragraph
: Normal paragram text.

You can set the properties of the selected text of a RichEdit with the property SelAttributes. You can set color, name of the font, style, and so on. For example, to set the selected text in bold, in addition of the other properties:

  RichEdit1.SelAttributes.Style := RichEdit1.SelAttributes.Style + [fsBold];

If text is selected, the attributes will be applied to the selected text and to newly inserted text.
If no text is selected, the attributes will be applied to newly inserted text.

You can select text and/or set the cursor in several ways:
- set SelStart and SelLength under program control;
- or select part of the text by dragging the mouse cursor over it;
- or select a word by double clicking it;
- or single click somewhere in the text: this sets SelStart to that position, and it sets SelLength to zero.

Example code for a RichEdit and two buttons:

// Set text to BOLD
procedure TForm1.btnBoldClick(Sender: TObject);
begin
  RichEdit1.SelAttributes.Style :=
    RichEdit1.SelAttributes.Style + [fsBold]; // add attribute BOLD
  RichEdit1.SetFocus;
end;

// Set text to ITALIC and BLUE
procedure TForm1.btnItalBlueClick(Sender: TObject);
begin
  RichEdit1.SelAttributes.Color := clBlue; // set color to BLUE
  RichEdit1.SelAttributes.Style :=
    RichEdit1.SelAttributes.Style + [fsItalic]; // add attribute ITALIC
  RichEdit1.SetFocus;
end;
 

 

Related Articles and Replies