[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

Problem in my function that find words in color in my richedit

Hello Guys, I have a function that find all words in my richedit that is in certain color, I am having a problem with it when I have two words in the same colour, to understand better, let me give you an example:

Imagine this sentence

"I am working in company today while my wife is at home with her syster baking a cake"

I
am
working  >> all are in clyellow color but

"company today" were selected and setted the yellow color,

My function gets this "company today" as a single words because the space between them are in yellow color too. I need that my function has a parameter to make this distinct between getting single
word in color or group word. I need this help

thanks

let me know if you didn't understand clear


procedure FindAllWordsInThisColour(RichEdit_: TRichEdit; colour: TColor; SL: TStrings);
var
  startpos, sellen, i, len: integer;
begin
  sl.beginupdate;
  try
  sl.clear;
//  len := Length( StringReplace(RichEdit_.Lines.Text,#13#10,#10,[rfReplaceAll, rfIgnoreCase]));
  len := length(RichEdit_.Lines.Text);
  startpos := 1;
  while startpos < len do
  begin
    RichEdit_.SelStart := startpos;
    RichEdit_.SelLength := 1;
    if (
         (RichEdit_.SelAttributes.Color = colour) and
         (RichEdit_.SelText <> '') and
         (RichEdit_.SelText <> #13) and
         (RichEdit_.SelText <> #10) and
         (RichEdit_.SelText <> #13#10)
        ) then
    begin
      sellen := 1;
      repeat
        inc(sellen);
        RichEdit_.SelLength := sellen;
      until ( (RichEdit_.SelAttributes.Color <> colour) or (sellen <> RichEdit_.SelLength) ); // indicates end
      if (sellen = RichEdit_.SelLength) then
        RichEdit_.SelLength := RichEdit_.SelLength - 1;
      sl.add(RichEdit_.SelText);
      startpos := startpos + RichEdit_.SelLength;
    end;
    inc(startpos);
  end;
  finally
    sl.endupdate;
  end;
end;
0
hidrau
Asked:
hidrau
1 Solution
 
Tomas Helgi JohannssonCommented:
    Hi!

Add this function to the unit

procedure SplitTextIntoWords(const S: string; words: TstringList);
var
  startpos, endpos: Integer;
begin
  Assert(Assigned(words));
  words.Clear;
  startpos := 1;
  while startpos <= Length(S) do
  begin
    while (startpos <= Length(S)) and not IsCharAlpha(S[startpos]) do
      Inc(startpos);
    if startpos <= Length(S) then
    begin
      endpos := startpos + 1;
      while (endpos <= Length(S)) and IsCharAlpha(S[endpos]) do
        Inc(endpos);
      words.Add(Copy(S, startpos, endpos - startpos));
      startpos := endpos + 1;
    end; { If }
  end; { While }
end; { SplitTextIntoWords }


and replace
sl.add(RichEdit_.SelText);
with
SplitTextIntoWords(RichEdit_.SelText,sl);

Hope this helps.

Regards,
   Tomas Helgi
0
 
hidrauAuthor Commented:
thanks very much
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now