We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Coloring words in memo or richedit

rincewind666
rincewind666 asked
on
Medium Priority
443 Views
Last Modified: 2013-11-22
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 :-)



Comment
Watch Question

Top Expert 2004
Commented:
you may take a look here

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

near the same question

meikl ;-)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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.

Commented:
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');

Commented:
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}

Top Expert 2004

Commented:
any feedback, rincewind666?
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.