Solved

How can I paint all words in my richedit ?

Posted on 2011-09-30
6
550 Views
Last Modified: 2012-05-12
Hello guys,

I have a richedit with loaded text and I have a stringlist of words.

I need an example with a function where the system could analyze and paint all the words of my text that is found in my StringList.

I use Delphi 7

thanks
Alex
0
Comment
Question by:hidrau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 100 total points
ID: 36892109
Here is a simple example:
procedure TForm1.FindText(FindStr: string; Culr:TColor);
var X, ToEnd : integer;
    oldCursor : TCursor;
begin
   oldCursor := Screen.Cursor;
   Screen.Cursor := crHourglass;
   with RichEdit1 do
   begin
     X := 0;
     ToEnd := length(Text) ;
     X := FindText(FindStr, X, ToEnd, []) ;
     while X <> -1 do
     begin
     SetFocus;
     SelStart := X;
     SelLength := length(FindStr) ;
     SelAttributes.Color := Culr;
     X := FindText(FindStr,X + length(FindStr),ToEnd, []);
     end;
   end;
   Screen.Cursor := oldCursor;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  strlst:TStrings;
  i:integer;
begin
  strlst:=TStringlist.create;
  strlst.Add('Whatever');
  for i:= 0 to strlst.Count-1 do
    begin
      FindText(strlst[i], clRed);
    end;
  strlst.Free;
end;

Open in new window


Search Function from about.com
0
 
LVL 9

Accepted Solution

by:
Mahdi78 earned 400 total points
ID: 36892248
You have to use this example with ListBox


procedure PaintMyWord(RE: TRichEdit; Words: TStrings);
var
I, J, CharPos, CharPos2, noChars: Integer;
MyWords: TStringList;
begin
MyWords := TStringList.Create;
 try
 MyWords.Text := Words.Text;
 for J := 0 to MyWords.Count - 1 do
  begin
  CharPos := 0; noChars := 0;
  for i := 0 to Pred(RE.Lines.Count) do
  noChars := noChars + Length(RE.Lines[i]);
  repeat
  CharPos2 := RE.FindText(MyWords[J], CharPos, noChars, [stWholeWord]);
  CharPos := CharPos2+1;
  RE.SelStart := CharPos2;
  RE.SelLength := Length(MyWords[J]);
  RE.SelAttributes.Color := clRed;
  until charpos = 0;
  end;
 finally
 MyWords.Free;
 end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
PaintMyWord(RichEdit1, ListBox1.Items);
end;

Open in new window

0
 
LVL 1

Author Comment

by:hidrau
ID: 36892279
Hi jimyX,

Yeah, it worked but there is a problem.

I need that the function finds only wholeword and it painted words being part of another word.

for example:

"my"

the function painted the my of Emmy. It must paint only my and skip Emmy or any other word that has the "my".

did you get it?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:hidrau
ID: 36892299
Hi Mahdi78,

you example was perfect.

thanks
0
 
LVL 9

Expert Comment

by:Mahdi78
ID: 36892300
Update...
you dont need to create TStringlist inside function


procedure PaintMyWord(RE: TRichEdit; Words: TStrings);
var
I, J, CharPos, CharPos2, noChars: Integer;
begin
 for J := 0 to Words.Count - 1 do
  begin
  CharPos := 0; noChars := 0;
  for i := 0 to Pred(RE.Lines.Count) do
  noChars := noChars + Length(RE.Lines[i]);
  repeat
  CharPos2 := RE.FindText(Words[J], CharPos, noChars, [stWholeWord]);
  CharPos := CharPos2+1;
  RE.SelStart := CharPos2;
  RE.SelLength := Length(Words[J]);
  RE.SelAttributes.Color := clRed;
  until charpos = 0;
  end;
end;

Open in new window

0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 36892305
thanks
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question