前へ検索








見てのとおり、前を検索は厳密にはなく、次を検索を重ねているに過ぎません
先頭から初めて検索する範囲の中で最後に見つかったものが結果になります
RichEdit1.HideSelection := False; にしないとスクロールしないことがあります

    procedure FindDialog1Find(Sender: TObject);
    procedure Find1Click(Sender: TObject);
    procedure FindNextButtonClick(Sender: TObject);
    procedure FindBackButtonClick(Sender: TObject);




function MyFindBackText(RichEdit: TRichEdit;const fs: String; StartPos, 
 SearchLength: Integer; Options: TSearchTypes): Integer;
var
  FoundAt, ffAt: LongInt;
  fStartPos, fToEnd: integer;
begin
   ffAt := 0;

  if RichEdit.FindText(fs,StartPos,SearchLength,Options)=-1 then
  begin
    Result := -1;
    exit;
  end;

  with RichEdit do
  begin
       fStartPos := StartPos;
    { ToEnd は検索範囲の文字数を示します }
    
    fToEnd := SearchLength- fStartPos; 
    FoundAt := FindText(fs, fStartPos, fToEnd, Options);

   While FoundAt <> -1 do
    begin
      ffAt := FoundAt;
      fToEnd := SearchLength - fStartPos; 
      FoundAt := FindText(fs, fStartPos, fToEnd, Options);
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(fs);
      fStartPos := FoundAt + Length(fs);
      if fToEnd<0 then
      begin
      ShowMessage('ありません');
      Break;
      end;
    end; 
   Result := ffAt;
  end;
end;

procedure TForm1.Find1Click(Sender: TObject);
begin
   if RichEdit1.SelLength <> 0 then
      begin
      FindDialog1.FindText := RichEdit1.SelText;
      end;

     FindDialog1.Execute;
end;

procedure TForm1.FindDialog1Find(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: integer;
begin
  with RichEdit1 do
  begin

  with Sender as TFindDialog do
   begin
    if frDown in Options then
    begin
      if SelLength <> 0 then
      StartPos := SelStart + SelLength
      else
      StartPos := 0;
     { ToEnd は検索範囲の文字数を示します }
      ToEnd := Length(Text) - StartPos;
      FoundAt := RichEdit1.FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
    end
    else
    begin
      StartPos := 0;
      ToEnd := SelStart;
      Lines.BeginUpdate;
      FoundAt := MyFindBackText(RichEdit1,FindDialog1.FindText, StartPos,ToEnd, [stMatchCase]);
      Lines.EndUpdate;
    end;
  end;
 if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(FindDialog1.FindText);
    end
    else
    begin
      //ShowMessage('もうありません');
      MessageBeep(MB_OK);
      Exit;
    end;
  end;
end;

procedure TForm1.FindNextButtonClick(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: integer;
begin
  with RichEdit1 do
  begin

    if SelLength <> 0 then
      StartPos := SelStart + SelLength
    else
      StartPos := 0;

    ToEnd := Length(Text) - StartPos;

    FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
    if FoundAt <> -1 then
    begin
      //なくても良い
      //SendMessage(RichEdit1.Handle,EM_SCROLLCARET,0,0);
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(FindDialog1.FindText);
    end
    else
    begin
      //ShowMessage('もうありません');
      MessageBeep(MB_OK);
      Exit;
    end;
  end;
end;

procedure TForm1.FindBackButtonClick(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: integer;
begin

  with RichEdit1 do
  begin
   StartPos := 0;
   ToEnd := SelStart;
   Lines.BeginUpdate;
   FoundAt := MyFindBackText(RichEdit1,FindDialog1.FindText, StartPos,ToEnd, [stMatchCase]);
   Lines.EndUpdate;
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(FindDialog1.FindText);
    end
    else
    begin
      //ShowMessage('もうありません');
      MessageBeep(MB_OK);
      Exit;
    end;
  end;
end;









Back