TRichEdit, Delphi, two colors in one line

panJames
panJames used Ask the Experts™
on
Hello experts,

I know how to add red line into TRichEdit:

RichEdit1.SelAttributes.Color := clRed;
RichEdit1.SelAttributes.Style := [fsBold];
RichEdit1.Lines.Add('xxx');

but how can I add line 'xxx' where first 'x' is green, second is 'yellow' and third one is 'pink'?

Thank you

panJames
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior developer, analyst and customer assistance
Commented:
procedure TForm1.Button1Click(Sender: TObject);
var
  i, x, y: Integer;
  Color: TColor;
begin
  x := RichEdit1.Lines.Add('xxx');
  y := RichEdit1.perform(EM_LINEINDEX, x, 0);
  RichEdit1.SelStart := y;
  for i := 0 to 2 do
  begin
    RichEdit1.SelStart := i+y;
    RichEdit1.SelLength := 1;
    case i of
      0:
        Color := clgreen;
      1:
        Color := clyellow;
      2:
        Color := clFuchsia;
    end;
    RichEdit1.SelAttributes.Color := Color;
    RichEdit1.SelAttributes.Style := [fsBold];
  end;
end;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial