Assuming you are building a richedit a phrase at a time, how do you organise things so that each new phrase has a different colour?
A phrase being a word or two, appended to an existing, currently last, line; or a new line.
Assuming you are building a richedit a phrase at a time, how do you organise things so that each new phrase has a different colour?
A phrase being a word or two, appended to an existing, currently last, line; or a new line.
Thanks to Claude …
procedure AddColoredPhrase(RichEdit: TRichEdit; const Phrase: string;
TextColor: TColor; NewLine: Boolean = False);
begin
// Move to the end of existing text
RichEdit.SelStart := Length(RichEdit.Text);
RichEdit.SelLength := 0;
// Add newline if requested
if NewLine and (RichEdit.Text <> '') then RichEdit.SelText := #13#10;
// Set the color for the new phrase
RichEdit.SelAttributes.Color := TextColor;
// Add the phrase
RichEdit.SelText := Phrase;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
RichEdit1.Clear;
// Build line with multiple colored phrases
AddColoredPhrase(RichEdit1, 'Hello', clRed);
AddColoredPhrase(RichEdit1, ' world', clBlue);
// Start a new line with different color
AddColoredPhrase(RichEdit1, 'This is', clGreen, True);
AddColoredPhrase(RichEdit1, ' great!', clPurple);
end;