Solved

How can I paint all words in my richedit ?

Posted on 2011-09-30
6
551 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month8 days, 10 hours left to enroll

615 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