Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Using F3 to FIND in a Richedit?



Permlink Replies: 1 - Last Post: Apr 20, 2018 1:54 PM Last Post By: Remy Lebeau (Te...
John Gray

Posts: 15
Registered: 10/3/99
Using F3 to FIND in a Richedit?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 19, 2018 3:54 PM
[Tokyo Starter in Win10]

I have a small text editor app, using a Richedit. There's a FIND procedure, based on code
in the Delphi DocWiki. The code's been modified to get search text from a TEdit on the form,
rather than through a FindDialog (similar to the Delphi IDE).

There is a button (btnFindNext) on the form that calls the FIND procedure. And, there is a
shorcut key [F3], linked to btnFindNext through a menu item.

This all works fine if I click the FindNext button (see code below) -- I can walk through all
occurrences of the search text. But... using F3, I get the first occurrence of the search text,
then nothing after that. If I use another shortcut key -- F(anything-but-3) -- then things work
again as expected.

It seems something is eating the F3 key -- is this a bug, or...?

Thanks,
John


//F3 shortcut key is linked to this button
procedure TfrmMain.btnFindNextClick(Sender: TObject);
begin
  if  edtFindNext.Text > '' then
    FindThis(rText, edtFindNext.Text)
  else
    showmessage('Nothing to find!');
end;
 
procedure TfrmMain.FindThis(ThisR : TRichEdit; ThisText : String);
var
  FoundAt: LongInt;
  StartPos, ToEnd: Integer;
  mySearchTypes : TSearchTypes;
begin
  //TSearchType = (stWholeWord, stMatchCase);
  mySearchTypes := [];
  with ThisR do
  begin
    Lines.BeginUpdate;
    { begin the search after the current selection if there is one }
    { otherwise, begin at the start of the text }
    if SelLength <> 0 then
       StartPos := SelStart + SelLength
    else
      StartPos := 0;
    { ToEnd is the length from StartPos to the end of the
      text in the rich edit control }
    ToEnd := Length(Text) - StartPos;
    FoundAt :=
      FindText(ThisText, StartPos, ToEnd, mySearchTypes);
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(ThisText);
    end
    else 
      showmessage(' - No more matches - ');
    Lines.EndUpdate;
  end;
end;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Using F3 to FIND in a Richedit?
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 20, 2018 1:54 PM   in response to: John Gray in response to: John Gray
John Gray wrote:

It seems something is eating the F3 key -- is this a bug, or...?

Works fine for me in earlier versions. I suggest you put a breakpoint
in the TApplication(Events).OnMessage event, and then step into the VCL
source code with the debugger to follow how the F3 key is actually
being processed.

Lines.BeginUpdate;

You don't need that, since you are not modifying the text.

StartPos := 0;

I would suggest using SelStart instead of 0.

ToEnd := Length(Text) - StartPos;

I would suggest using TRichEdit.GetTextLen() instead of
Length(TRichEdit.Text).

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02