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?
 
kretzschmarCommented:
you may take a look here

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

near the same question

meikl ;-)
0
 
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.
0
 
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');
0
 
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}

0
 
kretzschmarCommented:
any feedback, rincewind666?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.