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

Viewed 188

I want to display text in a TRichEdit using a for loop, but the only way I know how to display text in a RichEdit is by saying richedit.Lines.Add('blah blah blah'), but when I do this in a loop, each iteration displays its text on the next line instead of the same line as the previous iteration.

Is there a way to display text in a RichEdit using a loop in 1 continuous line, instead of it jumping to the next line each time it goes through the loop?

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;
3 Answers

To add text at the cursor position, use SelText. And use SelAttributes to change text attribute such as color of font.

Here an example that shows floating point values (Random array for the demo) in alternating red and black colors.

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;

Yes, it is possible. Use the SelStart property to move the caret to the desired position, use the SelLength property to clear any selection, and then use the SelText property to insert the new text at the current caret position. For example:

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;

If you want to manipulate certain lines you can do so by directly assigning desired string to specific line.

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';
Related