在TRichEdit的顶部插入彩色线 [英] Insert colored line at the top of TRichEdit
问题描述
我正在使用 TRichEdit
来显示应用程序中已完成的最后操作。我的 TRichEdit
的第一行应该是最后一个操作。如果操作失败,我想将此行标记为红色。
I'm using a TRichEdit
in order to show the last operations that have been done 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.
我的问题是我无法在 TRichEdit
的顶部插入彩色线条。这是我尝试过的方法:
My problem is that I am not able to insert a colored line at the top of my TRichEdit
. Here is what I've 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');
问题是我的 TRichEdit
仅适用第一次更改颜色,并保留所有行的颜色。如果我使用 Add()
而不是 Insert()
,颜色会发生变化,但是该行会插入到最后我的 TRichEdit
。
The problem is that my TRichEdit
only apply the first change of color and keep it for all the lines. If I use Add()
instead of Insert()
, the colors are changing but the line are inserted at the end of my TRichEdit
.
我的问题是:是否有一种简单的方法来获得我想要的结果?
推荐答案
如果要在开头插入,则需要将所选的开始和长度设置为0:
You need to set the selected start and 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');
或者,代替 RichEditLog.Lines.Insert()
,您可以将文本分配给 RichEdit.SelText
,但随后您需要自己添加换行符,例如:
Alternatively, instead of RichEditLog.Lines.Insert()
you can assign the text to RichEdit.SelText
, but then you need to add the new line characters yourself, f.ex.:
RichEditLog.SelText := 'Operation 1 OK'+sLineBreak;
无论哪种方式,应用于测试代码时,结果都是:
Either way, when applied to your test code the result is:
这篇关于在TRichEdit的顶部插入彩色线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!