Board index » delphi » Richedit: more colors in 1 lines

Richedit: more colors in 1 lines

I'm unable to set more than 1 color in the same line of a Richedit.
For example I'd like to write "Hello, World!"
Hello in Red, World in Green.
How can I do it?

Thanks

 

Re:Richedit: more colors in 1 lines


One way is to use the SelStart, SelLength, and SelAttributes :

  with RichEdit1 do
  begin
    Text := 'Hello World';
    SelStart := 0;
    SelLength := 5;
    SelAttributes.Color := clRed;
    SelStart := 6;
    SelLength := 6;
    SelAttributes.Color := clGreen;
    SelStart := Length(RichEdit1.Text);
    SelAttributes.Color := clWindowText;
  end;

Another way is to use raw formatting in a stream, then load the text
from the stream (this is what a lot of syntax editors do)

http://www.undu.com/Articles/981027d.html

Quote
Norberto wrote:

> I'm unable to set more than 1 color in the same line of a Richedit.
> For example I'd like to write "Hello, World!"
> Hello in Red, World in Green.
> How can I do it?

> Thanks

--

1 + 1 = 2  but  '1' + '1' = '11'

Re:Richedit: more colors in 1 lines


On Fri, 26 Jul 2002 17:46:07 -0400, Jellowe <j...@pirchworld.com>
wrote:

sorry but I must to insert color and text togheter, not replace the
attributes. Is not possible to do it?

Quote

>http://www.undu.com/Articles/981027d.html

>Norberto wrote:

>> I'm unable to set more than 1 color in the same line of a Richedit.
>> For example I'd like to write "Hello, World!"
>> Hello in Red, World in Green.
>> How can I do it?

>> Thanks

Re:Richedit: more colors in 1 lines


Quote
"Norberto" <no...@NOSPAMfreemail.it> wrote in message
> sorry but I must to insert color and text togheter, not replace the
> attributes. Is not possible to do it?

RichEdit1.SelAttreibutes.Color := theColor;
RichEdit1.SelText := theText;

Re:Richedit: more colors in 1 lines


On Fri, 26 Jul 2002 23:53:47 -0400, "Bruce Roberts"

Quote
<b...@bounceitattcanada.xnet> wrote:

>RichEdit1.SelAttreibutes.Color := theColor;
>RichEdit1.SelText := theText;

Sorry I have bad writted my question...
I'd like to do something like:

WriteRichedit('Text',Color);
WriteLineRichedit('Text',Color);

and not to modify the control after!

bye

Re:Richedit: more colors in 1 lines


Quote
"Norberto" <no...@NOSPAMfreemail.it> wrote in message
> I'd like to do something like:

> WriteRichedit('Text',Color);
> WriteLineRichedit('Text',Color);

> and not to modify the control after!

procedure tForm1.WriteRichEdit (const str : string; clr : tColor);

begin
RichEdit1.SelAttributes.Color := clr;
RichEdit1.SelText := str;
end;

procedure tForm1.WriteLnRichEdit (const str : string; clr : tColor);

begin
WriteRichEdit (str + #13 + #10, clr);
end;

Other Threads