Posted by webmaster Guido
In Reply to: New User. Help with text boxes.
To find out which key is pressed in a TEdit (edit-box), you've got to write a handler for its OnKeyPress event. An example:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); begin if Key = #13 then begin SelectNext(Sender as tWinControl, True, True ); if Sender = Edit10 then Button1.Click; // let the button click itself Key := #0; end; end;
When key #13 is pressed (Enter), SelectNext moves the input focus from the current control (in this case Edit1) to the next one. That is, if there is a "next" control in the same "container": if all your TEdits are directly put on Form1, than the "container" is Form1, so in this case Edit2 receives the focus (see note at end of message for other containers).
Important for this to work properly is the "tab order" of the
controls. Let's say that we have 4 TEdits and a button, then the tab order should be: Edit1 - Edit2 - Edit3 -
Edit 4 - Button1.
SelectNext finds the next child
control in the control?s tab order. If SelectNext can't find a "next" control (as defined by its parameters),
the focus remains with the current Control.
CurControl: current control, from which to begin
Note: if the controls
(TEdits, Tbuttons,...) are placed on a TPanel, that panel is the container, the "parent". Then,
Panel1.SelectNext moves to the next control that is on the panel.