?
Solved

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

Posted on 2014-09-22
2
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
Thommy earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

800 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