Capture The Word

Can someone here write a function or something to help me trap a word in a RichTextBox and change its formatting?

Like, for every "Hello" word in the RichTextBox, make it bold. For the rest, leave them alone.

Maybe the function can be something like:

FormatWord(WordToFormat, TheFormat)

Example: FormatWord(Hello, Bold)
The above will change every "Hello" in the RichTextBox to bold.

Thanks, I'm offering 200 points for the right answer.
jackjokerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi jackjoker,

try this

Procedure FormatWord(WordToFormat : String; TheFormat : TFontStyles; R : TRichEdit);
var
  I,
  OldSelStart,
  OldSelLength : Integer;
begin
  OldSelStart := R.SelStart;
  OldSelLength := R.SelLength;
  I := R.FindText(WordToFormat,0,MaxLongint,[]);
  while I > 0 do
  begin
    R.SelStart := I;
    R.SelLength := Length(WordToFormat);
    R.SelAttributes.Style := TheFormat;
    I := R.FindText(WordToFormat,I+1,MaxLongint,[]);
  end;
  R.SelStart := OldSelStart;
  R.SelLength := OldSelLength;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  FormatWord('Hello',[fsBold],Richedit1);
end;

meikl ;-)
0
LischkeCommented:
This is essentially an incremental search with addtional formatting. So you can use:

procedure MarkText(const S: String; Edit: TRichEdit; AColor: TColor);

var
  StartPos,
  EndPos,
  FoundAt: Integer;

begin
  with Edit do
  begin
    StartPos := 0; // or selection start or whatever
    repeat
      EndPos := Length(Text) - StartPos;
      FoundAt := FindText(S, StartPos, EndPos, [stMatchCase]);
      if FoundAt = -1 then Break;
      SelStart := FoundAt;
      SelLength := Length(S);
      SelAttributes.Color := AColor;
      SelAttributes.Style := [fsBold];
      StartPos := FoundAt + Length(S);
    until False;
    SelLength := 0;
  end;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
  Marktext('property', RichEdit1, clRed);
end;

Ciao, Mike
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LischkeCommented:
Sorry Meikl, just posted my answer a minute too late.

Ciao, Mike :-)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

kretzschmarCommented:
das macht doch nichts, mike ;-)
passiert mir auch manchmal.
0
LischkeCommented:
;-)
0
jackjokerAuthor Commented:
I'll tried all of your proposed answers. After evalution, I'll give the points to the right guy.

*smilez*
0
jackjokerAuthor Commented:
Thanks for the answer.

As for kretzschmar, your answer works too, but it if the word is starting on the first position of the line, your code won't work. So sorry.
0
kretzschmarCommented:
damn jackjoker, you've right

just replace this line
while I > 0 do
with
while I > -1 do

meikl ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.