?
Solved

Coloring words in memo or richedit

Posted on 2006-03-30
7
Medium Priority
?
427 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 :-)



0
Comment
Question by:rincewind666
  • 2
  • 2
5 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 2000 total points
ID: 16335806
you may take a look here

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

near the same question

meikl ;-)
0
 
LVL 1

Expert Comment

by:FarajLY
ID: 16348783
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
 
LVL 9

Expert Comment

by:bernani
ID: 16376404
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
 
LVL 9

Expert Comment

by:bernani
ID: 16376423
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 16390404
any feedback, rincewind666?
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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