devxlogo

Delphi 2.0 cursor handling

Delphi 2.0 cursor handling

Question:
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.

Answer:
Hmm… sounds like a serial number entry thing to me. Whatever the case,the best way I’ve found to handle this is to use the OnChange method of theTEdit to determine the length of the text being entered at runtime. Then ifthe text has reached a specified length, call Self.ActiveControl to move toa specific control.

For example, I have an application that requires thatthe user enter a social security number separated by three fields. Insteadof having the user press Tab to move from field to field, I trap the OnChangeevent handler to determine the length of the text and move to the next editcontrol if the text for the field in question reaches a certain size. Here’sthe 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 ofa social security number. As you may know, a social security number isdefined as a three-digit front plus a two-digit middle and a four-digit end. The codeabove captures this quite well.

Notice that I use the Tag property for the edit boxes. Each one has aspecific tag value so the procedure can determine which editbox is currently active, because TEdit(Sender) doesn’t tell you much. So byusing the Tag property, I can save myself a lot of coding by using a casestatement.

With respect to what decides whether the cursor should move, eachcase condition checks for the length of the current edit box’s text usingthe Length property. If has reached the size specified, the cursor is movedto the next field.

Why does this work? OnChange fires after a change has been made to anedit box. So this scheme works because the change has already been made andyou can readily measure the text size.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist