Link to home
Start Free TrialLog in
Avatar of Krieg
Krieg

asked on

Change the color withing TMemo?

How to change the color of the words within the paragraph using the TMemo?

Example:

   Memo1.text =
                The quick brown fox jumps over the lazy dog.
                The quick brown fox jumps over the lazy dog.

Results shoul be:

                "fox" = red in color(for first sentence)
                "lazy = blue in color(for next sentence)

   
 
Avatar of kretzschmar
kretzschmar
Flag of Germany image

first you must use a trichedit instead of a memo
(a tmemo can have only one color for its text)

sample follows

meikl ;-)
a sample

procedure TForm1.Button1Click(Sender: TObject);
begin
  richedit1.selstart := 0; //to begin
  //the first fox
  richedit1.selstart := richedit1.FindText('fox',richedit1.SelStart,length(richedit1.text),[stWholeWord]);
  if richedit1.selstart > 0 then
  begin
    richedit1.SelLength := length('fox');
    richedit1.SelAttributes.Color := clred;
  end;
  //the second lazy
  richedit1.selstart := 0; //to begin
  richedit1.selstart := richedit1.FindText('lazy',richedit1.SelStart,length(richedit1.text),[stWholeWord]);
  if richedit1.selstart > 0 then
    richedit1.selstart := richedit1.FindText('lazy',richedit1.SelStart+1,length(richedit1.text),[stWholeWord]);
  if richedit1.selstart > 0 then
  begin
    richedit1.SelLength := length('lazy');
    richedit1.SelAttributes.Color := clblue;
  end;
  //reset
  richedit1.selstart := 0; //to begin
  richedit1.SelLength := 0; //no selection
end;

meikl ;-)
On http://www.torry.net can be found a component named ColorMemo. Do this job. In same time here you can find anothers alike memo ( alike CoolMemo for example).
Avatar of Krieg
Krieg

ASKER

Hi miekl,

It's work fine here.
I have one more question. How will I going to get the word
one by one from trichedi1 and put it in the text file sequencially?

example:
   trichedit = the quick brown fox jumps over the lazy dog.

   text file = the
               quick
               brown
               fox
               .
               .
               .
               dog

I will increase the points to 40  
i think Editing Text, u first choice is TRichEdit.
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Krieg

ASKER

Thanks miekl, it works fine.

I've just encounter a small problem if you can help me regarding the trichedit color coded "words"

example:
    trichedit:   the quick brown fox jumps over
                 the quick brown fox jumps over

    every the same word should color this way:

    brown = should be colored blue
    fox   = should be colored red

    it should be the 2 brown will be colored blue,
    and it should also be the 2 fox will be colored red.

    because, right now the second line with the same sentence with the same word doesn't colored it the same color as what the first word of the sentence does. Because, I made a dynamic functions that will accept a word and colored the words to the specified color code.

    can you give me the solution as a bonus for me? Thanks in advance.

krieg          
                 
well, i hope i can,

a more generic sample

unit re_color_word_u;

interface

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

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Edit1: TEdit;
    Panel1: TPanel;
    Button1: TButton;
    ColorDialog1: TColorDialog;
    procedure Panel1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Panel1Click(Sender: TObject);
begin
  if colordialog1.execute then
    panel1.color := colordialog1.color;
end;

procedure colorword(ARichedit : TRichedit; AWord : String; AColor : TColor);
var
  foundAt,
  oldSelStart,
  oldSelLength : Integer;
begin
  oldSelStart := ARichedit.SelStart;
  oldSelLength := ARichedit.SelLength;
  try
    foundAt := -1;
    repeat
      foundAt := ARichEdit.FindText(AWord,foundAt+1,length(ARichEdit.Text),[stWholeWord]);
      if foundAt > -1 then
      begin
        ARichedit.SelStart := foundAt;
        ARichEdit.SelLength := length(AWord);
        ARichEdit.SelAttributes.Color := AColor;
      end;
    until foundAt = -1;
  finally
    ARichedit.SelStart := oldSelStart;
    ARichedit.SelLength := oldSelLength;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  colorword(richedit1,edit1.text,panel1.color);
end;

end.

hope thats it

meikl ;-)
Avatar of Krieg

ASKER

it really works, I didn't think this way

i know you can...

thanks meikl... :)


krieg