Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Busqueda con FindDialog hacia arriba (https://www.clubdelphi.com/foros/showthread.php?t=18262)

Arkaz 07-02-2005 18:44:07

Busqueda con FindDialog hacia arriba
 
Hola, estoy intentando realizar la busqueda de una palabra en un texto de un memo con FindDialog. He conseguido que me haga la busqueda hacia abajo con este codigo:

procedure TForm1.FindDialog1Find(Sender: TObject);
var
posicion:integer;
palabra:string;
begin
if frdown in FindDialog1.Options then begin
palabra:=Copy(Memo1.Text,
2+Memo1.SelStart,
Length(Memo1.Text)-Memo1.SelStart);

posicion:=Pos(UpperCase(finddialog1.FindText),UpperCase(palabra));

if posicion<>0 then
begin
Memo1.SetFocus;
Memo1.SelStart:=Memo1.SelStart+posicion;
Memo1.SelLength:=Length(finddialog1.FindText);
end
else begin
ShowMessage('No encuentro '+finddialog1.FindText);
memo1.SelStart:=0;
end;
end;
end;
end;

Pero la busqueda hacia arriba no me sale y no se como hacerla. Me podeis ayudar, por favor?.Gracias!

Lepe 08-02-2005 16:36:28

Esta función está sacada de las NKLIB, Tiene muchas otras para el tratamiento de strings, memos, nombres de archivos, etc...
Código Delphi [-]
// Habrá que buscar un algoritmo más eficiente para esto, de momento...
function RightPos(const cSubstr, s: String): Integer;
var
  i: Integer;
begin
  for i := Length(s) - Length(cSubStr) + 1 downto 1 do
  begin
    if Copy(s, i, Length(cSubstr)) = cSubStr then
    begin
      Result := i;
      exit
    end (*if*);
  end (*for*);
  Result := 0;
end (*RightPos*);

Saludos

Rowerto 20-04-2010 17:21:55

Ya se que llego con unos cuantos años de retraso pero creo que he logrado que la busqueda hacia arriba funcione.

Código Delphi [-]
var foundat, anterior : longint;
    startpos, toend : integer;
begin
    startpos:=0;
    foundat:=0;
    if richedit1.Sellength<>0 then
    begin
      toend := richedit1.SelStart + richedit1.SelLength;
    end
    else
    begin
      toend := length(richedit1.text);
    end;

    while startpos < toend do
    begin
      anterior := foundat;
      foundat := richedit1.findtext(finddialog1.findtext, startpos, toend, [stmatchcase]);
      if (anterior <>-1) then
      begin
        richedit1.SetFocus;
        richedit1.SelStart:=foundat;
        richedit1.SelLength:=length(finddialog1.FindText);
        startpos := richedit1.SelStart + richedit1.SelLength;
      end;
    end;

    if anterior = 0 then
    begin
    MessageDlg('No encuentro '+finddialog1.FindText+' Tal vez hayas llegado al principio.', mtInformation, [mbOk], 0, );
    end
    else
    begin
      richedit1.SetFocus;
      richedit1.SelStart:=anterior;
      richedit1.SelLength:=length(finddialog1.FindText);
      richedit1.Perform(EM_SCROLLCARET, 0, 0);
    end;
   end;

El unico problema es que a la hora de buscar como no le ponga la palabra exacta no me lo encuentra (se que es por el stmatchcase de la busqueda). Alguien me puede decir como puedo hacer?

Rowerto 20-04-2010 17:58:00

Como soy algo lento y no me di cuenta?

foundat := richedit1.findtext(finddialog1.findtext, startpos, toend, [stmatchcase]);

hay que ponerlo

foundat := richedit1.findtext(finddialog1.findtext, startpos, toend, []);

y ya esta, ya funciona sin tener que poner exactamente la palabra.


La franja horaria es GMT +2. Ahora son las 16:56:16.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi