Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Como reconocer ONExit hacia arriba o hacia abajo (https://www.clubdelphi.com/foros/showthread.php?t=86752)

oscarac 28-09-2014 06:41:37

Como reconocer ONExit hacia arriba o hacia abajo
 
buenas noches
Quiza el titulo no sea muy claro, la pregunta es la siguiente, como puedo identificar hacia donde va el control despues del ONExit cuando sale del Edit, quisiera saber si es que el control va al siguiente Edit o al anterior Edit

puedo salir con enter o con tab y el control pasa al siguiente edit, pero si presiono Shift+Tab el control va al Edit anterior, hay una forma de saberlo?

para que en el onexit solo valide el campo (en caso tenga algo que validar) cuando pase al siguiente y no cuando regrese al edit anterior

ecfisa 28-09-2014 09:16:35

Hola Oscar.

Fijate si te puede servir el modo que uso en este ejemplo para cualquier TWinControl o descendiente:
Código Delphi [-]
type
  TForm1 = class(TForm)
    ...
    Edit1: TEdit;
    Edit2: TEdit;
    ...
    procedure FormCreate(Sender: TObject);
  private
    FPrevFocusCtrl: TWinControl;
    procedure CMFocusChanged(var Msg: TCMFocusChanged); message CM_FOCUSCHANGED;
    procedure ActiveControlChange(Sender: TObject);
  public
  end;

var
  Form1: TForm1;

implementation 

uses TypInfo;

type
  TSentido = (seAnterior, seSiguiente);

var
  SentidoTab: TSentido; // (anterior/siguiente)

// Obtener sentido (anterior/siguiente)
procedure TForm1.CMFocusChanged(var Msg: TCMFocusChanged);
begin
  if Assigned(FPrevFocusCtrl) then
  begin
    if Msg.Sender.TabOrder > FPrevFocusCtrl.TabOrder then
      SentidoTab:= seSiguiente
    else
      SentidoTab:= seAnterior;
    FPrevFocusCtrl:= Self.ActiveControl;
  end;
  inherited;
end;

// Mostrar sentido (el valor ya se encuentra en SentidoTab)
procedure TForm1.ActiveControlChange(Sender: TObject);
begin
  Caption:= Copy(GetEnumName(TypeInfo(TSentido), Ord(SentidoTab)), 3, MaxInt);
  FPrevFocusCtrl:= Screen.ActiveControl;
end;

// Iniciar
procedure TForm1.FormCreate(Sender: TObject);
begin
  FPrevFocusCtrl:= Screen.ActiveControl;
  Screen.OnActiveControlChange:= ActiveControlChange;
end;
...

Saludos :)

nlsgarcia 28-09-2014 23:24:18

oscarac,

Cita:

Empezado por oscarac
...puedo salir con Tab y el control pasa al siguiente Edit...si presiono Shift + Tab el control va al Edit anterior...quisiera saber si es que el control va al siguiente Edit o al anterior Edit...¿Hay una forma de saberlo?...

:rolleyes:

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
  private
    { Private declarations }
    procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

// Detecta si se presiono la(s) tecla(s) Tab ó Shift + Tab hacia ó desde un Control TEdit
procedure TForm1.CMDialogKey(var Message: TCMDialogKey);
var
   State : TKeyboardState;

begin

   inherited;

   if (Screen.ActiveControl is TEdit) then
   begin

      GetKeyboardState(State);

      if ((State[VK_Shift] and 128) <> 0) and (Message.CharCode = VK_Tab) then
         ShowMessage('VK_Shift + VK_Tab ha sido presionada');

      if ((State[VK_Shift] and 128) = 0) and (Message.CharCode = VK_Tab) then
         ShowMessage('VK_Tab ha sido presionada');

   end;

end;

end.
El código anterior en Delphi 7 sobre Windows 7 Profesional x32, detecta si se presiono la(s) tecla(s) Tab ó Shift + Tab hacia ó desde un Control TEdit, ignorando dicho evento en el resto de los controles del formulario.

Espero sea útil :)

Nelson.

ecfisa 29-09-2014 10:59:12

Hola Oscar.

Ajustando el bosquejo anterior para su uso con los edits, quedaría:
Código Delphi [-]
...
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    ...
  private
    procedure ActiveControlChange(Sender: TObject);
  end;
...

implementation

uses TypInfo;

var
  PrevFocusCtrl: TWinControl;
  MaxTO: Integer = -2;

procedure TForm1.ActiveControlChange(Sender: TObject);
begin
  PrevFocusCtrl:= Screen.ActiveControl;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i, tbo:integer;
  PInfo: PPropInfo;
begin
  for i:= 0 to ControlCount-1 do
    if Assigned(GetPropInfo(Controls[i].ClassInfo, 'TabOrder')) then
    begin
      // Obtener máximo TabOrder
      tbo:= GetOrdProp(Controls[i], 'TabOrder');
      if tbo > MaxTO then MaxTO:= tbo;
      // Asignar evento a los edits
      if Controls[i].ClassType = TEdit then
        TEdit(Controls[i]).OnExit:= EditsExit;
    end;
  PrevFocusCtrl:= Screen.ActiveControl;
  Screen.OnActiveControlChange:= ActiveControlChange;
end;

procedure TForm1.EditsExit(Sender: TObject);
begin
  if (ActiveControl.TabOrder > PrevFocusCtrl.TabOrder) then
  begin
    // Aqui podes poner el proceso de validación, por ejemplo:
    try
      StrToFloat(TEdit(PrevFocusCtrl).Text);
    except
      PrevFocusCtrl.SetFocus;
      MessageBox(0,PChar('Error validando '+PrevFocusCtrl.Name),'',MB_ICONERROR);
    end;
  end;
end;
Excepto que no tengas en mente permitir el uso del mouse en la pantalla de ingreso... En cuyo caso bastaría con evaluar las combinaciones de teclas que indicas en tu mensaje.

Saludos :)


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

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