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.
: This will actually display the question on one line and the answer on the next 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.
1. Set the cursor at the end of the RichEdit.
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;
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;