| | |
Enter to tab
Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
Another way of trapping the Enter key is by using the form's KeyPreview property set to true. Then on the Form's onKeyPress event do this:
Be aware that TButtons and such will not throw this event handler. So the focus can not move to the next control when this type of component has focus. Memo boxes will get focus, but you have to use Shift+Enter for new lines in the memo. You can obviously curcumvent some of this behavior in the onKeyPress event, by examining the currently ActiveControl type or name.
This example obeys the TabOrder, and prevents you from writing a KeyPress event handler for each control that you want this behaviour.
--Jerry
Pascal and Delphi Syntax (Toggle Plain Text)
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char); var i:integer; tbOrder:Integer; nextComponent: TComponent; function getControlForNextTabOrder: TComponent; var iThisTab:Integer; n:Integer; begin result := nil; iThistab := ActiveControl.TabOrder; for n := 0 to ComponentCount -1 do if TWinControl(Components[n]).TabOrder = iThisTab +1 then begin result := Components[n]; break; end; end; begin if Key = #13 then begin nextComponent := getControlForNextTabOrder(); if (nextComponent <> nil) then begin ActiveControl := TWinControl(nextComponent); Key := #0; // prevent Key from propogating any further end; end; end;
Be aware that TButtons and such will not throw this event handler. So the focus can not move to the next control when this type of component has focus. Memo boxes will get focus, but you have to use Shift+Enter for new lines in the memo. You can obviously curcumvent some of this behavior in the onKeyPress event, by examining the currently ActiveControl type or name.
This example obeys the TabOrder, and prevents you from writing a KeyPress event handler for each control that you want this behaviour.
--Jerry
![]() |
Similar Threads
- using keyascii for OK (Visual Basic 4 / 5 / 6)
- Help! Got external program to open from VB6 but now?? (Visual Basic 4 / 5 / 6)
- dual boot system (Windows NT / 2000 / XP)
- tab control (tabPage) (C#)
- i want the user to enter an input without obligating him to press enter (C++)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Get ride off extra features in code
- Next Thread: code illiterate - add text from 5 TEdit to 1
| Thread Tools | Search this Thread |
Tag cloud for Pascal and Delphi





