Welcome, Twitter visitor! If you enjoy this post, don't hesitate to retweet!
Q&A: Delphi programming?
Question by Jigsaw K: Delphi programming?
Hello,
I’m making a program which consists of a RichEdit. I have set a function using StringFind and StringReplace to find and replace the word ‘cool’ in an example, the thing I want to do is to replace the color of the specific word cool. So in example:
all the colors were blue, green, cool <----- the word 'cool' must be blue in the example, so how can I replace color of an especific word in Delphi? Thank you very much, please answer as soon as possible.
Best answer:
Answer by reinandang
It’s easy, i’ll show you :
procedure TForm1.FindDialog1Find(Sender: TObject);
var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
with RichEdit1 do
begin
{ begin the search after the current selection if there is one }
{ otherwise, begin at the start of the text }
if SelLength <> 0 then
StartPos := SelStart + SelLength
else
StartPos := 0;
{ ToEnd is the length from StartPos to the end of the text in the rich edit control }
ToEnd := Length(Text) – StartPos;
FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(FindDialog1.FindText);
{Set Font Color here}
SelAttributes.Color:=clBlue;
end;
end;
end;
Add your own answer in the comments!























