Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

rich edit text colors

I wanted to have multiple colors on each line of my rich edit, how do i do this?

example.

'Hi there everyone!'
- I want 'Hi' to appear as blue and 'everyone' to appear in red.
How do i do this?
0
jchua
Asked:
jchua
  • 2
  • 2
  • 2
  • +2
1 Solution
 
LischkeCommented:
Select the text with:

  RichEdit1.SelStart := 100;
  RichEdit1.SelLenght := 100;

and then set the text attributes of the selected text like:

  RichEdit1.SelAttributes.Color := clRed;

Ciao, Mike
0
 
PeterLarsenCommented:
I dont think it's possible to change the background color in TRichedit.
Take a look into the demos : "...borland\delphiX\demos\richedit".
Peter
0
 
jchuaAuthor Commented:
Lischke,

it doesnt seem to work, di u have any other suggestion?
thanks!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
PeterLarsenCommented:
Mike's sample change the foreground color - and except from the spelling - it will change the foreground color.. :-)

"RichEdit1.SelLength := 100;"

Peter
0
 
LischkeCommented:
Peter,

yes, the forground color is changed with my code (despite the spelling error of "...Length"), but the question does not state about foreground or background colors. Just colors.

jchua,

WHAT does not work? Does the code start your browser or does the text line appear in green, although you specified red, or what else?

Ciao, Mike
0
 
kretzschmarCommented:
hi all,

an easy sample

procedure TForm1.Button1Click(Sender: TObject);
begin
  richedit1.Selstart := 0;
  Richedit1.SelAttributes.Color := clred;
  Richedit1.SetSelTextBuf('Hi');
  richedit1.Selstart := Length(Richedit1.Lines[0]);
  Richedit1.SelAttributes.Color := clBlack;
  Richedit1.SetSelTextBuf(' there ');
  richedit1.Selstart := Length(Richedit1.Lines[0]);
  Richedit1.SelAttributes.Color := clBlue;
  Richedit1.SetSelTextBuf('everyone');

end;

meikl
0
 
kretzschmarCommented:
just the same sample
except the colororder is now correct and now using the seltext-property instead of the setseltextbuf-method

procedure TForm1.Button1Click(Sender: TObject);
begin
  richedit1.Lines.Clear;
  richedit1.Selstart := 0;
  Richedit1.SelAttributes.Color := clBlue;
  Richedit1.SelText := 'Hi';
  richedit1.Selstart := Length(Richedit1.Text);
  Richedit1.SelAttributes.Color := clBlack;
  Richedit1.SelText := ' there ';
  richedit1.Selstart := Length(Richedit1.Text);
  Richedit1.SelAttributes.Color := clred;
  Richedit1.SelText := 'everyone';
end;

meikl
0
 
halserCommented:
The TRichEdit class in Delphi (D2 thru 5) implements a Richedit version 1 and this control does not support different background colors.

If you want to set the BACKGROUND color of the text in a richedit, you need at least a Richedit version 2 (the latest - version 3 - comes with W2K or Office2000). The DLL name of both - Richedit version 2 and version 3 is "Riched20.dll" and must be located in your \system directory.

That's the DLL part. Further you must implement the new functionality in Delphi. Luckily, there are already freeware implementations available.

Freeware implementation of Riched20.DLL (version 2): "Richedit98" from Alexander Obukhov. To download it, go to http://www.delphipages.com and search for "Richedit98".

Freeware implementation of Riched20.DLL (version 3): this is rather a tutorial by Robert Dunn. His home page is
http://home.att.net/~robertdunn/Yacs.html#RichEditPapers

Alex
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now