[Webinar] Streamline your web hosting managementRegister Today

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

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.
0
jackjoker
Asked:
jackjoker
  • 3
  • 3
  • 2
1 Solution
 
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
 
LischkeCommented:
Sorry Meikl, just posted my answer a minute too late.

Ciao, Mike :-)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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