Insert a colored line at the top of TRichEdit

I use TRichEdit to show the last actions that have been performed in my application. The first line of my TRichEdit should be the last operation. If the operation failed, I would like to put this line in red.

My problem is that I cannot insert a colored line at the top of my TRichEdit . Here is what I tried:

 RichEditLog.SelAttributes.Color := clBlack; RichEditLog.Lines.Insert(0, 'Operation 1 OK'); // RichEditLog.Lines.Add('Operation 1 OK'); RichEditLog.SelAttributes.Color := clRed; RichEditLog.Lines.Insert(0, 'Operation 2 failed'); // RichEditLog.Lines.Add('Operation 2 failed'); RichEditLog.SelAttributes.Color := clRed; RichEditLog.Lines.Insert(0, 'Operation 3 failed'); // RichEditLog.Lines.Add('Operation 3 failed'); RichEditLog.SelAttributes.Color := clBlack; RichEditLog.Lines.Insert(0, 'Operation 4 OK'); // RichEditLog.Lines.Add('Operation 4 OK'); 

The problem is that my TRichEdit only applies the first color change and saves it for all rows. If I use Add() instead of Insert() , the colors change, but the line is inserted at the end of my TRichEdit .

My question is: Is there an easy way to get the results I'm looking for?

+6
source share
2 answers

You need to set the selected start and the length to 0 if you want to insert at the beginning:

 RichEditLog.SelStart := 0; RichEditLog.SelLength := 0; RichEditLog.SelAttributes.Color := clBlack; RichEditLog.Lines.Insert(0, 'Operation 1 OK'); 

Alternatively, instead of RichEditLog.Lines.Insert() you can assign the text RichEdit.SelText , but then you need to add new line characters yourself, f.ex .:

 RichEditLog.SelText := 'Operation 1 OK'+sLineBreak; 

In any case, when applied to your test code, the result is:

enter image description here

+5
source

Have you tried playing with SelAttributes and SelText , SelStart , SelLenght ?

 Insert(0,'This is blue text.'); RichEdit1.SelStart := 0; RichEdit1.SelLenght := //end; RichEdit1.SelAttributes.Color := clBlue; 
+1
source

Source: https://habr.com/ru/post/1012029/


All Articles