unit RUTINAS;

interface
uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  DBCtrls, StdCtrls, Buttons, ExtCtrls, Menus, Mask, db,fmxutils;

    type TAplicacion= class(TThread)
         private
            fterminado:boolean;
            fappnom,fappparams:string;
            fmodo:integer;
         protected
             procedure Execute;override;
         public
         constructor create(appnom,appparams:string;modo:integer);
         procedure FinAplicacion(Sender:TObject);
         function Terminado:boolean;

    end;

    procedure CambiarControl(Sender: TObject;abajo:boolean);

implementation

procedure CambiarControl(Sender: TObject;Abajo:boolean);
var max,n,toactual,toorden:integer;
    anterior,siguiente,primero,ultimo:integer;
    toanterior,tosiguiente,toprimero,toultimo:integer;
begin
    if not(((Sender as tform).activecontrol)is tdbedit) then exit;

    toactual:=((Sender as tform).activecontrol).taborder;
    max:=(Sender as tform).componentcount;

    toanterior:=-1;
    tosiguiente:=max;
    toprimero:=max;
    toultimo:=-1;

    for n:=0 to max-1 do
       if (((Sender as tform).components[n])is tdbedit) then
         if (((((Sender as tform).components[n])as tdbedit).tabstop)
// anyado la siguiente linea porque cuando el control esta deshabilitado
// e intenta acceder me da error.
           and ((((Sender as tform).components[n])as tdbedit).enabled))
         then
         begin

             toorden:=(((Sender as tform).components[n] as twincontrol).taborder);
             if toorden>toultimo then
             begin
                 ultimo:=n;
                 toultimo:=toorden;
             end;

             if toorden<toprimero then
             begin
                 primero:=n;
                 toprimero:=toorden;
             end;

             if (toorden>toactual) and (toorden<tosiguiente) then
             begin
                 siguiente:=n;
                 tosiguiente:=toorden;
             end;

             if (toorden<toactual) and (toorden>toanterior) then
             begin
                 anterior:=n;
                 toanterior:=toorden;
             end;
         end;

         if abajo then
             if tosiguiente<max then (((Sender as tform).components[siguiente]) as TWinControl).setfocus
                               else if toprimero<max then (((Sender as tform).components[primero]) as TWinControl).setfocus;

         if not abajo then
             if toanterior>-1 then (((Sender as tform).components[anterior]) as TWinControl).setfocus
                               else if toultimo>-1 then (((Sender as tform).components[ultimo]) as TWinControl).setfocus;

end;

constructor TAplicacion.Create(appnom,appparams:string;modo:integer);
begin
   fterminado:=false;
   fappnom:=appnom;
   fappparams:=appparams;
//   freeonterminate:=true;
   inherited create(false);
end;

procedure TAplicacion.FinAplicacion(Sender:TObject);
begin
    fterminado:=true;
end;

function TAplicacion.terminado:boolean;
begin
   result:=fterminado;
end;

procedure TAplicacion.Execute;
begin
     Executefile (fappnom,fappparams,'',fmodo);
end;
end.


