Re: Displaying text in a TRichEdit?


Posted by webmaster Guido on February 22, 2001 at 14:22:36:

In Reply to: Displaying text in a TRichEdit? posted by Jeff on February 20, 2001 at 22:07:26:

: I need to display one line to a RichEdit with part of line diplaying in Bold font.
: Here is an example, but it will display on 2 separate lines, but I need to display on one line:
: WHAT IS YOUR NAME? JOHN SMITH
: Display this on one line with John Smith in Bold font?
:
: procedure TForm1.Button1Click(Sender: Object);
: var question, answer : String;
: begin
: question := 'What is your name?';
: answer := 'John Smith';
: //Display the question to RichEdit which is set for Regular Font
: RichEdit1.Lines.Add(question);
: //set RichEdit to Bold font
: RichEdit1.SelAttributes.Style := [fsBold];
: //display answer to RichEdit
: RichEdit1.Lines.Add(answer);
: //set RichEdit back to Regular Font
: RichEdit1.SelAttributes.Style :=
: RichEdit1.SelAttributes.Style - [fsBold];
: end;

: This will actually display the question on one line and the answer on the next line.
: I need to display both on the same line.
---------------------------

You can add text to a RichEdit in several ways: by adding a line with Lines.Add, by replacing selected text with other text, and so on. Further on, the font properties of the added text depend from various things, not only from the default font properties of the RichEdit.
Here's a possible solution:

1. Set the cursor at the end of the RichEdit.
2. Set the desired font properties.
3. Add the first string with Lines.Add().
4. Put the cursor at the end of the RichEdit.
5. If a CR/LF is found here, put the cursor before this CR/LF (from the old days: CR= carriage return= code to go to beginning of line; LF= linefeed= code to go to next line).
6. Set the desired font properties.
7. Replace the selected text with the second string (if we don't select any text, nothing is replaced, but the string is inserted at the position of the cursor).

What's all that CR/LF stuff? It's because Lines.Add() will add a string in three different ways:

- if the RichEdit is empty, the string is added, followed by a CR/LF;
- if the RichEdit ends with a CR/LF, only the string is added;
- if the RichEdit contains text and it does not end with a CR/LF, a CR/LF is added, followed by the string.

This means that, after we have used Lines.Add(), there can be either a CR/LF or some other character at the end of the RichEdit.

The code for this solution:

procedure TForm1.SpeedButton5Click(Sender: TObject);
var
  Question, Answer: string;
begin
  Question := 'What is your name?';
  Answer := 'John Smith';
  with RichEdit1 do begin
    SelStart := Length(RichEdit1.Text);  // 1
    SelAttributes.Style := [];           // 2
    Lines.Add(Question + ' ');           // 3
    SelStart := Length(RichEdit1.Text);  // 4
    if Text[Length(RichEdit1.Text)] = #10 then
      SelStart := SelStart - 2;          // 5
    SelAttributes.Style := [fsBold];     // 6
    SelText := Answer;                   // 7
  end;
end;


Related Articles and Replies: