Solved

How can I paint all words in my richedit ?

Posted on 2011-09-30
6
520 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now