添加新文本时如何防止TRichMemo重置文本属性 [英] How to prevent TRichMemo from resetting text attributes when you add new text

查看:85
本文介绍了添加新文本时如何防止TRichMemo重置文本属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TRichMemo对象,该对象在运行时创建并填充文本.

I have a TRichMemo object which I create and populate with text at runtime.

我有一个计时器,每10秒触发一次功能.该函数看起来像这样:

I have a timer that triggers a function each 10 seconds. The function looks something like this:

procedure TServerSideForm.NewLineTimerTimer(Sender: TObject);
var
  timeForward: TDateTime;

  timerText: wideString;

  startRange, endRange: longInt;
begin
  timeForward := Time;
  timeForward := IncSecond(timeForward, ServerSideForm.NewLineTimer.Interval div 1000);

  //...

  timerText := TimeToStr(Time) + ' - ' + TimeToStr(timeForward);   


  startRange := Length(WindowMemo.Text);

  WindowMemo.Text := WindowMemo.Text + sLineBreak + sLineBreak + timerText + sLineBreak + sLineBreak;

  endRange := Length(WindowMemo.Text) - 1;


  WindowMemo.SetRangeColor(startRange, endRange, clGreen);

  //...
end;    

一切正常,所需范围内的文本变为绿色.

Everything works perfectly, text in the desired range becomes green.

但是,只要我在TRichMemo中添加一些新文本,所有内容就会重置为黑色文本.

But as soon as I add some new text to my TRichMemo, everything resets back to black text.

为什么会这样?有没有办法防止这种重置的发生?

Why is this happening? Is there a way to prevent this reset from happening?

P.S当我使用SetRangeParams函数时,也会发生同样的情况.

P.S Same situation happens, when I use the SetRangeParams function.

推荐答案

使用Append方法而不是访问type StringText,因为它仅保留文字,而不保留格式.

Use Append method instead of accessing a type String value Text as it keeps only the literals not the format.

更改

WindowMemo.Text := WindowMemo.Text + sLineBreak + sLineBreak + timerText + sLineBreak + sLineBreak;

使用

WindowMemo.Append(sLineBreak + sLineBreak + timerText + sLineBreak + sLineBreak); 

方法2

如果您决定添加不带换行符的文本,则可以将所提到的行替换为

Should you decide to add text withot line breaks you can replace the mentioned line with

uses RichMemoUtils;
...    
InsertColorStyledText(WindowMemo,timerText,Random($FFFFFF),[],Length(WindowMemo.Text) -1);

这篇关于添加新文本时如何防止TRichMemo重置文本属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆