Solved

I need a help with my function that finds a word in richedit and change its color

Posted on 2014-09-22
2
188 Views
Last Modified: 2014-09-23
Hello guys

I have a function that is not working so fine.

I'd like to run all of over my text and look for a determine word in determine color and change it to another color.

I did a function, but it doesn't work the way that it should be.

Maybe a hand can help me to reach my goal.

This is my code:

procedure ChangeWordColor(RE: TRichEdit; TheWord: String; Color: TColor; Style: TFontStyles);
var
  i, CharPos, noChars: Integer;
begin
  CharPos := 0;
  noChars := 0;
  for i := 0 to Pred(RE.Lines.Count) do
  Begin
    noChars := noChars + Length(RE.Lines[i]);
    CharPos := RE.FindText(TheWord, CharPos, noChars, [stWholeWord]);
    RE.SelStart := CharPos;
    RE.SelLength := Length(TheWord);

    if RE.SelAttributes.Color = clRed then
    Begin
      RE.SelAttributes.Color := color;
      RE.SelAttributes.Style := Style;
    End;
  End;
end;

Open in new window


Thanks
Alex
0
Comment
Question by:hidrau
2 Comments
 
LVL 19

Accepted Solution

by:
Thommy earned 500 total points
Comment Utility
Try this...
Procedure ChangeWordColor(RE:TRichEdit;TheWord:string; TheColor: TColor; TheStyle: TFontStyles);
Var
FoundAt : integer;
len: integer;
start_pos: integer;
begin
    start_pos:=0;
    len:=RE.GetTextLen - Start_Pos;
    FoundAt:=RE.FindText(TheWord,start_pos,len,[stWholeWord]);
    while FoundAt <> -1 do
    begin
      RE.SelStart := FoundAt;
      RE.SelLength := Length(TheWord);
//      if RE.SelAttributes.Color = clRed then Begin
        RE.SelAttributes.Style := TheStyle;
        RE.SelAttributes.Color := TheColor;
        RE.SelText :=TheWord;
//      End;
      start_pos:=RE.SelStart+RE.SelLength;
      len:=RE.GetTextLen - Start_Pos;
      FoundAt:=RE.FindText(TheWord,start_pos,len,[stWholeWord]);
    end;
end;

Open in new window

0
 
LVL 1

Author Closing Comment

by:hidrau
Comment Utility
Thanks a lot,

I saw what I need to change.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now