Solved

How can I paint all words in my richedit ?

Posted on 2011-09-30
6
547 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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