FindDialog - procurando ou localizando uma string dentro de um memo

Top  Previous  Next

// Exemplo completo de como usar o FINDDIALOG do Delphi.

 

// Esta função é usada em conjunto com o Dialog do Windows (FindDialog)

// Passe o Memo1.Text no StrOri e a string a localizar no StrLoc

// PosInicial       = é de onde parte a pesquisa

// DifMaieMin       = é para diferencial maiusculas e minusculas.

// ParaCima         = vai da posicao inicial para cima.

// CoincidirPalavra = encontra somente palavra inteira

function Localizar(const StrOri, StrLoc: string; const PosInicial: Longint; DifMaieMin: Boolean = False;

                   ParaCima: Boolean = False; CoincidirPalavra: Boolean = False): Longint;

var

  I    : Longint;

  Achou: Boolean;

 

  // esta rotina confere de a letra anterior/posterior a palavra fecha com vazio ou não letra-numero.

  procedure ConferePalavraInteira;

  begin

    if Achou and CoincidirPalavra then

    begin

      if ((IfThen(I = 0'', Copy(StrOri, I - 1             , 1)) <> ''and (Copy(StrOri, I              - 11)[1in ['0'..'9','A'..'Z','a'..'z'])) or

         ((IfThen(I = 0'', Copy(StrOri, I + Length(StrLoc), 1)) <> ''and (Copy(StrOri, I + Length(StrLoc), 1)[1in ['0'..'9','A'..'Z','a'..'z'])) then

        Achou := False;

    end;

  end;

 

begin

  Result := -1;

  if ParaCima then // se for para cima ele faz o for (loop) diminuindo o valor.

  begin

    for I := PosInicial - Length(StrLoc) downto 0 do

    begin

      if DifMaieMin then // a var achou deve ser TRUE para sair do looping achando a string

        Achou := StrLoc = Copy(StrOri, I, Length(StrLoc))

      else

        Achou := AnsiUpperCase(StrLoc) = AnsiUpperCase(Copy(StrOri, I, Length(StrLoc)));

 

      ConferePalavraInteira;

 

      if Achou then

      begin

        Result := I - 1// contém a POSICAO do bicho.

        if Result < 0 then Result := 0;

        Break;

      end;

    end;

  end

  else  // Normal, do cursor para baixo

    for I := PosInicial to (Length(StrOri) - Length(StrLoc) + 1) do

    begin

      if DifMaieMin then

        Achou := StrLoc = Copy(StrOri, I, Length(StrLoc))

      else

        Achou := AnsiUpperCase(StrLoc) = AnsiUpperCase(Copy(StrOri, I, Length(StrLoc)));

 

      ConferePalavraInteira;

 

      if Achou then

      begin

        Result := I - 1;

        if Result < 0 then Result := 0;

        Break;

      end;

    end;

end;

 

////////////// botao que inicia procura

procedure TForm1.LocalizarBtnClick(Sender: TObject);

begin

  FindDialog1.FindText := Edit1.Text;

  FindDialog1.Options  := [frDown,frShowHelp];

  FindDialog1.Execute;

end;

 

///////////// evento do component FindDialog

procedure TForm1.FindDialog1Find(Sender: TObject);

var

  P: Integer;

begin

  FindDialog1.CloseDialog;

  P:= Localizar( Memo1.Text, FindDialog1.FindText, Memo1.SelStart + Memo1.SelLength,

                 frMatchCase in FindDialog1.Options, not (frDown in FindDialog1.Options), frWholeWord in FindDialog1.Options);

  if P > -1 then

  begin

    Memo1.SelStart  := P;

    Memo1.SelLength := Length(FindDialog1.FindText);

    Memo1.SetFocus;

  end

  else

    ShowMessage('Fim da Pesquisa!');

end;

 

////////////// form

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);

begin

  // Ctrl+F ou Ctrl+F3 abre a tela de localizar

  // F3 = caso já esteja procurando, repete pesquisa senão abre também a tela.

  if Key = Vk_F3 then

    if (FindDialog1.FindText <> ''and (not (ssCtrl in Shift)) then

      FindDialog1Find(Self)

    else

      FindDialog1.Execute;

  // Ctrl + F abre a pesquisa denovo

  if ((Key = 70or (Key = 102)) and (ssCtrl in Shift) then FindDialog1.Execute;

end;