Solved

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

Posted on 2014-09-22
2
193 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
ID: 40338561
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
ID: 40338882
Thanks a lot,

I saw what I need to change.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

786 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