Solved

How can I paint all words in my richedit ?

Posted on 2011-09-30
6
549 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
  • 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
Independent Software Vendors: 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

685 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