有什么方法可以使用循环在 RichEdit 中的 1 个连续行中显示文本?

Is there any way to display text in 1 continuous line in a RichEdit using a loop?

我想使用 for 循环在 TRichEdit 中显示文本,但我知道如何在 RichEdit 中显示文本的唯一方法是说 richedit.Lines.Add('blah blah blah'),但是当我在循环中执行此操作,每次迭代都在下一行显示其文本,而不是与上一次迭代显示在同一行。

有没有一种方法可以在 RichEdit 中使用连续一行的循环来显示文本,而不是每次循环时都跳到下一行?

for i := 1 to 7 do
begin
  if (arrScores[i] <> MinValue(arrScores)) OR (arrScores[i] <> MaxValue(arrScores)) then
    begin
      redOut.Lines.Add(FloatToStr(arrScores[i]));
    end;

如果你想操纵某些行,你可以通过直接将所需的字符串分配给特定行来实现。

RichEdit1.Lines[0] := 'Some text that is to be shown in fist line';
RichEdit1.Lines[1] := 'Some text that is to be shown in second line';

要在光标位置添加文本,请使用 SelText。并使用 SelAttributes 更改文本属性,例如字体颜色。

这里是一个以红色和黑色交替显示浮点值(演示中的随机数组)的示例。

procedure TForm1.Button1Click(Sender: TObject);
var
    I         : Integer;
    arrScores : array [1..7] of double;
begin
    Randomize;
    for I := 1 to 7 do
        arrScores[I] := Random;

    RichEdit1.Clear;
    for I := 1 to 7 do begin
        if (I and 1) = 0 then
            RichEdit1.SelAttributes.Color := clRed
        else
            RichEdit1.SelAttributes.Color := clBlack;
        RichEdit1.SelText := Format('%6.2f ', [arrScores[I]]);
    end;
end;

是的,这是可能的。使用 SelStart 属性 将插入符号移动到所需位置,使用 SelLength 属性 清除任何选择,然后使用 SelText 属性 在当前插入符位置插入新文本。例如:

redOut.Clear;
for i := 1 to 7 do
begin
  if (arrScores[i] <> MinValue(arrScores)) or (arrScores[i] <> MaxValue(arrScores)) then
  begin
    redOut.SelStart := redOut.GetTextLen;
    redOut.SelLength := 0;
    redOut.SelText := FloatToStr(arrScores[i]);
  end;