The Road to Delphi

Delphi – Free Pascal – Oxygene

How to mark all the instances of a word in a TRichEdit using Delphi

Leave a comment

the Nect code shows how to mark all the instances of a word in a TRichEdit using Delphi


procedure MarkString(RichEdit:TRichEdit;const strtomark : string);
Var
FoundAt : integer;
begin
    FoundAt:=RichEdit.FindText(strtomark,0,maxInt,[stWholeWord]);
    while FoundAt <> -1 do
    begin
             RichEdit.SelStart := FoundAt;
             RichEdit.SelLength := Length(strtomark);
             RichEdit.SelAttributes.Style := [fsBold];
             RichEdit.SelAttributes.Color := clRed;
             RichEdit.SelText :=strtomark;
             FoundAt:=RichEdit.FindText(strtomark,FoundAt + length(strtomark),maxInt,[stWholeWord]);
    end;
end;


procedure UnMarkString(RichEdit:TRichEdit;const strtomark : string);
Var
FoundAt : integer;
begin
    FoundAt:=RichEdit.FindText(strtomark,0,maxInt,[stWholeWord]);
    while FoundAt <> -1 do
    begin
             RichEdit.SelStart := FoundAt;
             RichEdit.SelLength := Length(strtomark);
             RichEdit.SelAttributes.Style := [];
             RichEdit.SelAttributes.Color := clBlack;
             RichEdit.SelText :=strtomark;
             FoundAt:=RichEdit.FindText(strtomark,FoundAt + length(strtomark),maxInt,[stWholeWord]);
    end;
end;


MarkString(RichEdit1,'delphi'); //To Mark a string

UnMarkString(RichEdit1,'delphi'); //To UnMark a string


Author: Rodrigo

Just another Delphi guy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s