Coloring words in memo or richedit

I have a list of words. I load a text file into a memo or richedit. I want the words from this list to be colored.

Example:

The list of words:
fox
dog

Load Memo1 or Richedit1 text with
The quick brown fox jumps over the lazy dog.

In this case the words "fox" and "dog" will be red and the rest black.

I am using Delphi 6.  This is URGENT so I am giving the maximum 500 points for this - and my grateful thanks :-)



rincewind666Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
you may take a look here

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20609678.html

near the same question

meikl ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FarajLYCommented:
try this
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button2: TButton;
    ListBox1: TListBox;
    Button3: TButton;
    Button4: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
function SearchForText_AndSelect(RichEdit: TRichEdit; SearchText: string): Boolean;
var
  StartPos, Position, Endpos: Integer;
begin
  StartPos := 0;
  with RichEdit do
  begin
    Endpos := Length(RichEdit.Text);
    Lines.BeginUpdate;
    while FindText(SearchText, StartPos, Endpos, [stMatchCase])<>-1 do
    begin
      Endpos   := Length(RichEdit.Text) - startpos;
      Position := FindText(SearchText, StartPos, Endpos, [stMatchCase]);
      Inc(StartPos, Length(SearchText));
      SetFocus;
      SelStart  := Position;
      SelLength := Length(SearchText);

     SelAttributes.Color := clRED;
     SelAttributes.Size := 8;
     SelAttributes.Name := 'MS Sans Serif';

    end;
    Lines.EndUpdate;
  end;
end;

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
var i :integer;
    s:string;
begin
    // Search in Richedit
  for i:=0 to ListBox1.Items.Count -1 do
    SearchForText_AndSelect(RichEdit1,ListBox1.Items.Strings[i] );
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
RichEdit1.Lines.LoadFromFile('c:\lc.txt');
end;

procedure TForm1.Button4Click(Sender: TObject);
 var s:string;
begin
 s:=InputBox('Insert list Word','','') ;
 if s<>'' then ListBox1.Items.Add(s);
end;

end.
bernaniCommented:
Maybe this could help

function SearchForText_AndSelect(RichEdit: RichEdit; SearchText: string): boolean;
var
  k, startpos, position, endpos: integer;
begin
  startpos := 0;
  with RichEdit do
    begin
    endpos := Length(RichEdit.Text);
    Lines.BeginUpdate;
    while FindText(SearchText, startpos, endpos, [stMatchCase])<>-1 do
    begin
      endpos := Length(RichEdit.Text) - startpos;
      position := FindText(SearchText, startpos, endpos, [stMatchCase]);
      Inc(startpos, Length(SearchText));
      SetFocus;
      SelStart := position;
      SelLength := Length(SearchText);
        for k:=0 to Length(SearchText) do
            begin
            Richedit.DefAttributes.Color := clblack;
            RichEdit.DefAttributes.Style := [];
            Richedit.SelAttributes.Color := clRed;
            RichEdit.SelAttributes.Style := [fsBold];
            end;
      end;
    Richedit.SelStart:= 0;
    Lines.EndUpdate;
  end;
 end;


call like this:

SearchForText_AndSelect(Richedit1,'fox');
SearchForText_AndSelect(Richedit1,'dog');
bernaniCommented:
A little typo. Replace the declaration
function SearchForText_AndSelect(RichEdit: RichEdit; SearchText: string): boolean;
with
function SearchForText_AndSelect(RichEdit: TRichEdit; SearchText: string): boolean; {TRichedit instead of RichEdit}

kretzschmarCommented:
any feedback, rincewind666?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.