Pass the focus when it exceed the capacity of a TEdit  

Send By: Q3 Team
Web : www.q3.nu
Email: dlib@q3.nu
Date: 07/12/99

Tip accessed 705 times

 


When we try to exceed the capacity of a TEdit, this send a mesage EN_MAXTEXT to your parent window to inform it.

We must capture said event, and to pass the focus to next control:

In the form definition, we put:


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



and later, in the implementation:


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





To make the same thing, but without losing the character typed when entering in the new TEdit:

  • Add a global variable in the form:


     var
       Form1: TForm1;
       Letra:char;
    



  • Put the KeyPreview property of the form to true
  • Put the next code in the OnKeyPress event of the form:


       Letra:=Key;
    



  • And change the prior message treatment procedure by this another:


     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;
    





    Updated at 19/08/99
    There was a typographic error: WM_NEXTCTLDLG instead of WM_NEXTDLGCTL...


    Updated at 20/08/99
    I add the second example that doesn't lose characters when passing the following TEdit