Re: Re: Re: Displaying text in a TRichEdit?


Posted by webmaster Guido on February 23, 2001 at 15:01:54:

In Reply to: Re: Re: Displaying text in a TRichEdit? posted by Jeff on February 22, 2001 at 16:33:41:

: I figured out how to do it a little different. What do you think about this?
:
: procedure TForm1.SpeedButton5Click(Sender: TObject);
: var Question, Answer : String;
: begin
:  Question := 'What is your name';
:  Answer := 'John Smith';
:  RichEdit1.SelAttributes.Style := [fsBold];
:  RichEdit1.SelText := Question;
:  RichEdit1.SelAttributes.Style :=
:  RichEdit1.SelAttributes.Style - [fsBold];
:  RichEdit1.SelText := ' ' + Answer;
:  RichEdit1.Lines.Add('');
: end;

-------------

That certainly will work, but the result is not always the same. With SelText :=... you replace the selected text with a new text, and if nothing is selected, it inserts text at the position of the cursor. So, your code only gives the desired results under certain starting conditions:

- the cursor must be at the end of the RichEdit;
- the last codes in the RichEdit must be CR/LF.

If you are not always 100% sure of the starting conditions, you could start the code with these two steps:

1. If the RichEdit is not empty and there is no CR/LF at the end, add a CR/LF (add an empty line);
2. Set the cursor at the end of the RichEdit.