1. Home
  2. Computing & Technology
  3. Delphi Programming

Moving the focus to the next edit control
when MaxLength characters have been reached

By , About.com Guide

The MaxLenght property of a TCustomEdit descedant components (TEdit, TDBEdit, ...) specifies the maximum number of characters the user can enter into the edit control.
Here's how to move immediately to the next control (in the tab order) when the max character length has been met:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.Edit1Change(Sender: TObject) ;
begin
   if Sender is TCustomEdit then
   with Sender as TCustomEdit do
   if MaxLength = GetTextLen then
     self.SelectNext(TCustomEdit(Sender), true, true) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Note 1: the above code is a handler of the OnChange event of an edit control named Edit1.
Note 2: MaxLength property is ideally used in database applications where MaxLength of a TDBEdit component matches a TField's Size property (for string type fields).

More TEdit tips: "Use arrow keys to move between controls" and "Using the ENTER key as TAB".

Delphi tips navigator:
» How to handle the TMaskEdit's internal "Invalid input value. Use escape key to abandon changes" error in Delphi
« Raising TPageControl's OnChagning and OnChange events from code

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TEdit, TMaskEdit
  6. Moving the focus to the next Delphi edit control when MaxLength characters have been reached

©2009 About.com, a part of The New York Times Company.

All rights reserved.