Data Entry: Automagically Moving the Cursor
How can I get the cursor to advance to the next form field after a field has been filled with the maximum number of characters? I am using Delphi 2.0.
Hmm... sounds like a serial number or SSN entry thing to me. Whatever the case, the best way I've found to handle this is to use the OnChange method of the TEdit to determine the length of the text being entered at runtime. Then if the text has reached a specified length, call Self.ActiveControl to move to a specific control.
For example, I have an application that requires that the user enter a social security number separated by three fields. Instead of having the user press Tab to move from field to field, I trap the OnChange event handler to determine the length of the text and move to the next edit control if the text for the field in question reaches a certain size. Here's the code for my social security number entry:
procedure PatientForm.edSSNBegChange(Sender: TObject); begin case TEdit(Sender).Tag of 100 : if Length(TEdit(Sender).Text) = 3 then ActiveControl := edSSNMid; 101 : if Length(TEdit(Sender).Text) = 2 then ActiveControl := edSSNEnd; 102 : if Length(TEdit(Sender).Text) = 4 then ActiveControl := Button1; end; end;
On the form where this method lives, I have three edit boxes called edSSNBeg, edSSNMid and edSSNEnd to signify the first, middle, and end parts of a social security number. As you may know, a social security number is defined as a three-digit front plus a two-digit middle and a four-digit end. The code above captures this quite well.
Notice that I use the Tag property for the edit boxes. Each one has a specific tag value so the procedure can determine which edit box is currently active, because TEdit(Sender) doesn't tell you much. So by using the Tag property, I can save myself a lot of coding by using a case statement.
With respect to what decides whether the cursor should move, each case condition checks for the length of the current edit box's text using the Length property. If has reached the size specified, the cursor is moved to the next field.
Why does this work? OnChange fires after a change has been made to an edit box. So this scheme works because the change has already been made and you can readily measure the text size.
Copyright © 1997 Brendan V. Delumpa All Rights Reserved
|Copyright © The Delphi Corner 2001 All Rights Reserved