Ver Mensaje Individual
  #6  
Antiguo 26-11-2004
Avatar de Lepe
[Lepe] Lepe is offline
Miembro Premium
 
Registrado: may 2003
Posts: 7.424
Reputación: 28
Lepe Va por buen camino
autocompleta DBEdit

en Firebird, puede que tengas que hacer un query.last para que recupere todos los registros, pero en fin, esto es para tablas paradox.

Código Delphi [-]
  // constantes para el AutoCompleta
  const NO_BUSCAR = 1;
        BUSCAR = 0;
        BUSCAR_SI_CAMBIA = 2; // si no lo encuentra la 1ª vez, no sigue buscando hasta
                             //  que se borre caracteres anteriores
procedure AutoCompleta( Ctr:TcustomEdit; qry:TDataset;const NombreCampo:string); overload;
procedure AutoCompleta( Ctr:TComboBox; qry:TDataset;const NombreCampo:string);overload;


procedure AutoCompleta(Ctr:TcustomEdit;  qry:TDataset;const NombreCampo:string);
const checking :Boolean = False;
      OldText:string='';

var Campo:TField;
begin
  case key of
  vk_DELETE,VK_BACK :
            begin
              Ctr.SelText := '';
              Ctr.Tag:= codigoutil.IfThen(Ctr.Tag=BUSCAR_SI_CAMBIA,BUSCAR, NO_BUSCAR);
            end;
  VK_LEFT,VK_CAPITAL, VK_RIGHT, VK_HOME, VK_END: Ctr.Tag:= NO_BUSCAR;
  else
    Ctr.Tag := codigoutil.IfThen(Ctr.Tag=BUSCAR_SI_CAMBIA,NO_BUSCAR, BUSCAR);
  end;

  if Ctr.Tag = BUSCAR then
  begin
    if checking then Exit;
    checking:= True;
    if (Ctr.SelStart = 0) then
      OldText := Ctr.Text
    else
    OldText:= Copy(Ctr.Text,1,Ctr.SelStart);//+Key;
    if not qry.Active then
      qry.Open;
    if qry.Locate(NombreCampo, OldText  ,[locaseinsensitive, lopartialkey]) then
    begin
      Campo :=qry.FindField(NombreCampo);
      if Campo = nil then
        raise Exception.Create('proc AutoCompleta: Campo '+QuotedStr(NombreCampo)+' no encontrado')
      else
      begin
        if Key = vk_return then
          Ctr.Text:= Campo.AsString
        else
          Ctr.Text:= OldText + Rightstr(Campo.AsString,Length(Campo.AsString)-Length(OldText));
        Ctr.SelStart:= Length(OldText);
        Ctr.SelLength := Length(Ctr.Text);
  //      SetCaretPos(Length(OldText),1);
        Ctr.Tag:= BUSCAR;
      end;
    end
    else
    begin
      Ctr.Tag:= BUSCAR_SI_CAMBIA;
    end;
    checking:= False;

  end;


end;

En cuanto a eficiencia [...] es otro tema, pero si lo pide el usuario, pues se hace.

La funcion codigoutil.ifthen es la misma que tienes en delphi, pero segun los parámetros, estan desperdigadas por varias units, buscalas en la ayuda.

Esto funciona para un Edit, DBEdit, etc.

Saludos

Última edición por Lepe fecha: 10-08-2005 a las 12:56:11. Razón: faltaba un "raise"
Responder Con Cita