Pasar el foco al sobrepasar la capacidad de un TEdit  

Enviado Por: Q3 Team
Web : www.q3.nu
Email: dlib@q3.nu
Fecha: 07/12/99

Truco accedido 106 veces

 


Cuando intentamos sobrepasar la capacidad de un TEdit, éste envia un mensaje EN_MAXTEXT a su ventana padre para informarle.

Nosotros tendremos que capturar dicho evento, y pasar el foco al siguiente control:

En la definicion de la form ponemos:


 Type
      TForm1 = class(TForm)
      ..........
      private
        procedure WMCommand(var Msg: TWMCommand); message WM_COMMAND;
      ..........
      end;



y luego, en el implementation:


    procedure TForm1.WMCommand(var Msg: TWMCommand);
    begin
      if Msg.NotifyCode = EN_MAXTEXT then
      begin
         PostMessage(Handle, WM_NEXTDLGCTL,0, 0);
         inherited;
      end;
    end;





Para hacer lo mismo, pero sin perder el caracter tecleado al entrar en el nuevo TEdit:

  • Añadimos una variable global a la form:


     var
       Form1: TForm1;
       Letra:char;
    



  • Ponemos la propiedad Keypreview de la form a true
  • Ponemos el siguiente código en evento OnKeyPress de la form:


       Letra:=Key;
    



  • Y como procedure de tratamiento del mensaje substituimos la anterior por esta otra:


     procedure TForm1.WMCommand(var Msg: TWMCommand);
     begin
       if Msg.NotifyCode = EN_MAXTEXT then
       begin
         SendMessage(Handle, WM_NEXTDLGCTL , 0, 0);
         If ActiveControl is TEdit then
           with TEdit(ActiveControl) do
           begin
              Text:=Letra;
              SelStart:=2;
           end;
         inherited;
       end;
     end;
    





    Actualizado el 19/08/99
    Habia un error tipográfico: WM_NEXTCTLDLG en lugar de WM_NEXTDLGCTL ...


    Actualizado el 20/08/99
    Añado el segundo ejemplo, que no pierde caracteres al pasar al siguiente TEdit