Board index » delphi » RichEdit and Colors

RichEdit and Colors

Hello,

I am having trouble figuring out an easy, yet effective, way of coloring all instances of NUMBERS in a RichEdit when I
use: RichEdit1.LoadFromFile(FileName);

I have tried case, if..then...else, and even searched for components. I even tried using SelStart and such, but with no
real luck (app mostly hangs like in an endless loop).

Does anyone have a tried and true method? Or maybe some hints?

Thanks!

Ryan

 

Re:RichEdit and Colors


Quote
"Ryan Styles" wrote:
> Hello,

> I am having trouble figuring out an easy, yet effective, way of

coloring all instances of NUMBERS in a RichEdit when I
Quote
> use: RichEdit1.LoadFromFile(FileName);

> I have tried case, if..then...else, and even searched for

components. I even tried using SelStart and such, but with no

Quote
> real luck (app mostly hangs like in an endless loop).

I'm not sure what you mean with NUMBERS here ( 0..9 or the string
"NUMBERS" ),
so here is _two_ functions - One for each of the two tasks.
Hope it helps.

{--------------------}
procedure ColorNumbers(aRichEdit: TRichEdit; aColor: TColor);
var
  S: string;
  ix,
  TxtLen: Integer;
  s1, s2: Integer;
begin
  S := aRichEdit.Text;
  ix := 1;
  TxtLen := Length(S);
  while ix <= TxtLen do
  begin
    while (ix <= TxtLen) and (not (S[ix] in ['0'..'9'])) do inc(ix);
    s1 := ix;
    while (ix <= TxtLen) and (S[ix] in ['0'..'9']) do inc(ix);
    s2 := ix;
    aRichEdit.Perform(EM_SETSEL, s1 - 1, s2 - 1);
    aRichEdit.SelAttributes.Color := aColor;
  end;
end;
{--------------------}
procedure ColorStrInstances(aRichEdit: TRichEdit; const aString:
string; aColor: TColor);
var
  ix, SrcLen, TxtLen: Integer;
begin
  SrcLen := aRichEdit.GetTextLen;
  TxtLen := Length(aString);
  ix := 0;
  repeat
    ix := aRichEdit.FindText(aString, ix, SrcLen, []);
    if ix >= 0 then
    begin
      aRichEdit.Perform(EM_SETSEL, ix, ix + TxtLen);
      aRichEdit.SelAttributes.Color := aColor;
      Inc(ix, TxtLen);
    end;
  until ix < 0;
end;
{--------------------}

Regards
-ThomasN

Re:RichEdit and Colors


Quote
>I'm not sure what you mean with NUMBERS here ( 0..9 or the string
>"NUMBERS" ),
>so here is _two_ functions - One for each of the two tasks.
>Hope it helps.

Yes, it was numbers as in '0 through 9' that I wanted to highlight.

Quote
>{--------------------}
>procedure ColorNumbers(aRichEdit: TRichEdit; aColor: TColor);
>var
>  S: string;
>  ix,
>  TxtLen: Integer;
>  s1, s2: Integer;
>begin
>  S := aRichEdit.Text;
>  ix := 1;
>  TxtLen := Length(S);
>  while ix <= TxtLen do
>  begin
>    while (ix <= TxtLen) and (not (S[ix] in ['0'..'9'])) do inc(ix);
>    s1 := ix;
>    while (ix <= TxtLen) and (S[ix] in ['0'..'9']) do inc(ix);
>    s2 := ix;
>    aRichEdit.Perform(EM_SETSEL, s1 - 1, s2 - 1);
>    aRichEdit.SelAttributes.Color := aColor;
>  end;
>end;

This was exactly what I wanted to do! Everything I had tried to implement was not nearly as structured as your code is,
but it was close. My main problems appear to have been the fact that I was not using the right statements in key areas
of the code. This has helped me to learn what I was doing wrong, and I thank you very much for that.

- Show quoted text -

Quote
>{--------------------}
>procedure ColorStrInstances(aRichEdit: TRichEdit; const aString:
>string; aColor: TColor);
>var
>  ix, SrcLen, TxtLen: Integer;
>begin
>  SrcLen := aRichEdit.GetTextLen;
>  TxtLen := Length(aString);
>  ix := 0;
>  repeat
>    ix := aRichEdit.FindText(aString, ix, SrcLen, []);
>    if ix >= 0 then
>    begin
>      aRichEdit.Perform(EM_SETSEL, ix, ix + TxtLen);
>      aRichEdit.SelAttributes.Color := aColor;
>      Inc(ix, TxtLen);
>    end;
>  until ix < 0;
>end;
>{--------------------}

This procedure also works well. I may have use for it in the future.  =)

Very many thanks,

Ryan

Other Threads