FindNext in RichEdit

I am trying to use the FindDialog to search for multiple instances of a word in my document. When I execute the FindDialog and type in my word I want the document to highlight the word and bring that part of the document into the window. If I hit "FindNext" I want it to CONTINUE searching the document.

What its doing now is finding the first instance of the word ONLY.

Help???

Michael
mjbcomp@ureach.com
mjbcompAsked:
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.

IndefreiCommented:
Hi, Michael
try this

Suchtext : String
InfoSuchErfolg   :Integer;
.....
.....
procedure TInfoForm1.FindInfotext(Sender: TObject);
var
   SelPos: LongInt;
   StartPos, ToEndPos: Integer;
   SuchArt : TSearchTypes98;
begin
{  with editor do
  begin
    selpos := Pos(Suchtext, Text) - 1;

    if (SelPos <> 0) then
    begin
      SetFocus;
      SelStart := SelPos;
      SelLength := Length(SuchText);
      InfoSuchErfolg:=1;
    end
    else
    begin
      InfoSuchErfolg:=0;
    end;
  end;

//  TSearchType = (stWholeWord, stMatchCase);
TSearchTypes = set of TSearchType;
}
   with editor do
   begin
      Suchart :=[];
      If TextSucheRichtung > 0 then   //search direction
      begin
           if SelLength<>0 then
           begin
                StartPos:=SelStart+SelLength
           end
           else
           begin
                StartPos:=0;
           end;
           ToEndPos:=Length(Text)-StartPos;
      end
      else
      begin
           if SelLength<>0 then
           begin
                StartPos:=SelStart-SelLength
           end
           else
           begin
                StartPos:=Length(Text);
           end;
           Suchart :=Suchart+[stBackward];
           ToEndPos:=0;
      end;


      SelPos :=FindText(SuchText, StartPos, ToEndPos, SuchArt);

      If Selpos <> -1 then
      begin
          SetFocus;
        SelStart:=SelPos;
        SelLength:=Length(SuchText);
        InfoSuchErfolg:=1;    // Success
     end
     else
     begin
               InfoSuchErfolg:=0;  // No Success
     end;
   end;
end;

good luck
Indi
0
IndefreiCommented:
Oops, did not see, it's for richedit98
 SuchArt : TSearchTypes98;
but must be simular, perhaps Tsearchtypes, can't get it right now, sorry
Indi
0
men xinCEOCommented:
From Delphi 5's Example:

procedure TForm1.FindDialog1Find(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: Integer;
begin
  with RichEdit1 do
  begin
    { 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(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(FindDialog1.FindText);
    end;
  end;
end;

menxin

0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

kretzschmarCommented:
hi mjbcomp,

my version from my paq

works only with a richedit not with a memo
its full featured with the default find and replace dialog

(MatchCase or not, WholeWord or not, Replace all or single)


unit m_f_r_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, RichEdit, ComCtrls;

type
  TForm1 = class(TForm)
    ReplaceDialog1: TReplaceDialog;
    Button1: TButton;
    Memo1: TRichEdit;
    procedure ReplaceDialog1Close(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ReplaceDialog1Show(Sender: TObject);
    procedure ReplaceDialog1Find(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

var
  FoundAt : Integer;
  FT : TFindText;
  OldS, OldSL : Integer;

{$R *.DFM}

procedure TForm1.ReplaceDialog1Close(Sender: TObject);
begin
  Memo1.SelStart := OldS;
  Memo1.SelLength := OldSL;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  ReplaceDialog1.FindText := Memo1.SelText;
  ReplaceDialog1.Execute;
end;

procedure TForm1.ReplaceDialog1Show(Sender: TObject);
begin
  OldS := Memo1.SelStart;
  OldSL := Memo1.SelLength;
end;

procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  MatchC, WholeW : Integer;
  fs : String;
begin
  fs := ReplaceDialog1.FindText;
  FT.chrg.cpMin := Memo1.SelStart;
  FT.chrg.cpMax := Length(Memo1.Text);
  FT.lpstrText := PChar(fs);
  If frWholeWord in ReplaceDialog1.Options then
    WholeW := FT_WHOLEWORD
  else
    WholeW := 0;
  If frMatchCase in ReplaceDialog1.Options then
    MatchC := FT_MATCHCASE
  else
    MatchC := 0;
  FoundAt := Memo1.Perform(EM_FindText,MatchC or WholeW,integer(@FT));
  if FoundAt > -1 then
  begin
    memo1.SelStart := FoundAt;
    memo1.SelLength := length(fs);
    If frReplaceAll in ReplaceDialog1.Options then
      ReplaceDialog1Replace(Self);
  end
  else  ShowMessage(fs+' is never found');
  memo1.SetFocus;
end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
  If (FoundAt = Memo1.SelStart) and
     (Memo1.SelLength = Length(ReplaceDialog1.Findtext)) then
    Memo1.SelText := ReplaceDialog1.Replacetext;
  ReplaceDialog1Find(Self);
end;

end.

meikl
0
kretzschmarCommented:
appendix,

if you only wnat to find with the finddialog, then use this procedure

procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  MatchC, WholeW : Integer;
  fs : String;
begin
  fs := ReplaceDialog1.FindText;
  FT.chrg.cpMin := Memo1.SelStart;
  FT.chrg.cpMax := Length(Memo1.Text);
  FT.lpstrText := PChar(fs);
  If frWholeWord in ReplaceDialog1.Options then
    WholeW := FT_WHOLEWORD
  else
    WholeW := 0;
  If frMatchCase in ReplaceDialog1.Options then
    MatchC := FT_MATCHCASE
  else
    MatchC := 0;
  FoundAt := Memo1.Perform(EM_FindText,MatchC or WholeW,integer(@FT));
  if FoundAt > -1 then
  begin
    memo1.SelStart := FoundAt;
    memo1.SelLength := length(fs);
{    If frReplaceAll in ReplaceDialog1.Options then
      ReplaceDialog1Replace(Self); } //this is then not needed
  end
  else  ShowMessage(fs+' is never found');
  memo1.SetFocus;
end;

meikl
0
IndefreiCommented:
Hi, Michael
It's definitively TSearchTypes for richedit

Hi, menxin
Fine comment, duplicates , but also translates my code (sure vice versa first)
except that i use my own find formular.

Hi meikl
Do you have this for memos also ?
Can't get the text to be scrolled automatically, when memotext longer and searchtext found in the non visible area of the memo, even when setting focus again.
(would give 30 pts also)
cheers
Indi
0
kretzschmarCommented:
hi indi,

kram kram, stöber, stöber ah ja da

well a memo is not so nice to search
try this

unit f_m_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    FindDialog1: TFindDialog;
    procedure Button1Click(Sender: TObject);
    procedure FindDialog1Find(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
var
  oldpos : integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  oldpos := memo1.selstart;
  Finddialog1.FindText := copy(Memo1.text,memo1.selstart+1,memo1.sellength);
  FindDialog1.Execute;
end;

procedure TForm1.FindDialog1Find(Sender: TObject);
var
  FCase : Integer;
  SelTmp: Integer;
  SelPos: Integer;
begin
  if frmatchcase in Finddialog1.Options then
    fCase := 1
  else fcase := 0;
  if Memo1.sellength > 0 then
    Selpos := Memo1.SelStart + Memo1.SelLength
  else
    SelPos := Memo1.SelStart;
  case fCase of
    0 : Begin
          if selpos = 0 then
            Selpos := pos(Uppercase(FindDialog1.FindText),UpperCase(memo1.text))
          else
          begin
            SelTmp := Selpos;
            Selpos := pos(UpperCase(FindDialog1.FindText),
                          copy(UpperCase(memo1.text),selpos,length(memo1.text)-selpos )) - 1;
            if selpos > 0 then selpos := selpos + seltmp;
          end;
        end;
    1 : Begin
          if selpos = 0 then
            Selpos := pos(FindDialog1.FindText,memo1.text)
          else
          begin
            SelTmp := Selpos;
            Selpos := pos(FindDialog1.FindText,
                          copy(memo1.text,selpos,length(memo1.text)- selpos )) - 1;

            if selpos > 0 then selpos := selpos + seltmp;
          end;
        end;
  end;
  if SelPos > 0 then
  begin
    Memo1.SelStart := SelPos - 1;
    Memo1.SelLength := Length(FindDialog1.FindText);
  end
  else MessageDlg(FindDialog1.FindText + ' not found in Memo1', mtError, [mbOk], 0);
  memo1.setfocus;
end;

end.

meikl
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
IndefreiCommented:
hi, meikl,
works great (supposing, memo has vertical bars) ,
Gonna post a q 4 u
Indi

0
mjbcompAuthor Commented:
Kretzschmar,

Thank you so much for your help. My program works great now.

mjbcomp.
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.