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)
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)
a sample
procedure TForm1.Button1Click(Sender : TObject);
begin
richedit1.selstart := 0; //to begin
//the first fox
richedit1.selstart := richedit1.FindText('fox',r ichedit1.S elStart,le ngth(riche dit1.text) ,[stWholeW ord]);
if richedit1.selstart > 0 then
begin
richedit1.SelLength := length('fox');
richedit1.SelAttributes.Co lor := clred;
end;
//the second lazy
richedit1.selstart := 0; //to begin
richedit1.selstart := richedit1.FindText('lazy', richedit1. SelStart,l ength(rich edit1.text ),[stWhole Word]);
if richedit1.selstart > 0 then
richedit1.selstart := richedit1.FindText('lazy', richedit1. SelStart+1 ,length(ri chedit1.te xt),[stWho leWord]);
if richedit1.selstart > 0 then
begin
richedit1.SelLength := length('lazy');
richedit1.SelAttributes.Co lor := clblue;
end;
//reset
richedit1.selstart := 0; //to begin
richedit1.SelLength := 0; //no selection
end;
meikl ;-)
procedure TForm1.Button1Click(Sender
begin
richedit1.selstart := 0; //to begin
//the first fox
richedit1.selstart := richedit1.FindText('fox',r
if richedit1.selstart > 0 then
begin
richedit1.SelLength := length('fox');
richedit1.SelAttributes.Co
end;
//the second lazy
richedit1.selstart := 0; //to begin
richedit1.selstart := richedit1.FindText('lazy',
if richedit1.selstart > 0 then
richedit1.selstart := richedit1.FindText('lazy',
if richedit1.selstart > 0 then
begin
richedit1.SelLength := length('lazy');
richedit1.SelAttributes.Co
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).
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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,f oundAt+1,l ength(ARic hEdit.Text ),[stWhole Word]);
if foundAt > -1 then
begin
ARichedit.SelStart := foundAt;
ARichEdit.SelLength := length(AWord);
ARichEdit.SelAttributes.Co lor := AColor;
end;
until foundAt = -1;
finally
ARichedit.SelStart := oldSelStart;
ARichedit.SelLength := oldSelLength;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
colorword(richedit1,edit1. text,panel 1.color);
end;
end.
hope thats it
meikl ;-)
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:
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,f
if foundAt > -1 then
begin
ARichedit.SelStart := foundAt;
ARichEdit.SelLength := length(AWord);
ARichEdit.SelAttributes.Co
end;
until foundAt = -1;
finally
ARichedit.SelStart := oldSelStart;
ARichedit.SelLength := oldSelLength;
end;
end;
procedure TForm1.Button1Click(Sender
begin
colorword(richedit1,edit1.
end;
end.
hope thats it
meikl ;-)
ASKER
it really works, I didn't think this way
i know you can...
thanks meikl... :)
krieg
i know you can...
thanks meikl... :)
krieg
(a tmemo can have only one color for its text)
sample follows
meikl ;-)