我想使用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;
要在光标位置添加文本,请使用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;
如果您想操作某些行,可以直接将所需字符串分配给特定行。
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';