auto skip on input forms

Dave_B_C
Dave_B_C used Ask the Experts™
on
I have restricted user input to the maximum characters allowed within edit fields on an input form but want to autoskip to the next field rather than receive an error beep.. How do I achieve this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
OnKeyPress event of TEdit put:

IF TEdit(Sender).MaxLength=Length(TEdit(Sender).Text) THEN
BEGIN
  MyForm.SelectNext(TWinControl(Sender),TRUE,TRUE);
  Key:=#0;
END;

where MyForm is name of your form.

Author

Commented:
I'll give it a whirl, and if I get it to work then the points are yours, thanks!

Dave
Another version but the same idea :-(

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if (Sender is TEdit) then
    with (Sender as TEdit) do
      if MaxLength = Length(Text) then begin
        SelectNext(TWinControl(Sender),true,true);
        Key:=#0;
      end;
end;

Regards, Geo
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
This autoskips OK, but loses the first character in the next field. eg if max length = 4 and user types 123456 I want 1234 in field 1 and 56 in field 2. I get just 6 in field 2...?

Dave
Commented:
You must simulate keyboard press
so OnKeyPress:

IF TEdit(Sender).MaxLength=Length(TEdit(Sender).Text) THEN
BEGIN
 SelectNext(TWinControl(Sender),TRUE,TRUE);
 keybd_event(Ord(Key), 0, 0, 0);
 key:=#0;
END;

Maybe you would want also to make sure that text on field2 is entered after text already in field2 so you should add OnEnter event (to set cursor position to the end of current text in edit):

  TEdit(Sender).SelStart:=Length(TEdit(Sender).Text);

Roza is right with one little addition:
...
keybd_event(Ord(Key), 0, 0, 0); // key down event
keybd_event(Ord(Key), 0, KEYEVENTF_KEYUP, 0); // key up event
key:=#0;
...

Author

Commented:
Works well, thanks... Dave

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial