Check all instances of a word (Delphi, RichText)
Ok, this is the problem that was grunt and I can't see to find a definitive answer. How do you find and mark all instances of a word?
I mean, I'm looking for a word (say: Person). If a word exists, I mark (using red or any color) all instances of that word in richedit. If I press Esc it will be canceled.
Any ideas?
is estimated.
source to share
wonderer, I wrote this code, I hope it will be helpful:
Procedure MarkString(RichEdit:TRichEdit;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;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
Bye.
source to share
The following code will search for all occurrences of a given word (case sensitive) in an advanced editing control, change the font color to red, and finally restore the control's original selection (all with as little flicker as possible). ):
procedure TForm1.FindWord(const AWord: string; AOptions: TSearchTypes);
var
OrigSelStart, OrigSelLen: integer;
Start, Found: integer;
begin
if AWord = '' then
exit;
OrigSelStart := RichEdit1.SelStart;
OrigSelLen := RichEdit1.SelLength;
RichEdit1.Perform(WM_SETREDRAW, 0, 0);
try
Start := 0;
Found := RichEdit1.FindText(AWord, Start, MaxInt, AOptions);
while Found <> -1 do begin
RichEdit1.SelStart := Found;
RichEdit1.SelLength := Length(AWord);
// TODO: save start of search match and original font colour
RichEdit1.SelAttributes.Color := clRed;
Start := Found + Length(AWord);
Found := RichEdit1.FindText(AWord, Start, MaxInt, AOptions);
end;
finally
RichEdit1.SelStart := OrigSelStart;
RichEdit1.SelLength := OrigSelLen;
RichEdit1.Perform(WM_SETREDRAW, 1, 0);
RichEdit1.Repaint;
end;
end;
Now you need to store the matches along with the attributes of the original text in a list and use the information in that list to revert all changes when clicked Esc
. However, this can be quite tricky if you think the matches could contain different font styles, colors, etc. So I haven't provided any code to preserve formatting, it depends on your requirements.
Oh, make sure the highlighted matches are removed before the text can be changed again, otherwise you won't be able to restore the original text formatting correctly.
source to share