How to prevent TRichMemo from resetting text attributes when you add new text

筅森魡賤 提交于 2019-12-11 04:47:32

问题


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

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.

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 Same situation happens, when I use the SetRangeParams function.


回答1:


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

Change

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

with

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

METHOD 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);


来源:https://stackoverflow.com/questions/47215188/how-to-prevent-trichmemo-from-resetting-text-attributes-when-you-add-new-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!