Author: Alex Schlecht
Standard-behaviour of controls when pressing ENTER-KEY is a BEEP-Sound. But how to
write Components which handle ENTER-Key like TAB-Key?
Answer:
This Article will show how to write Components which will handle the ENTER-Key in
the same behaviour like the TAB-Key. As Sample I will take a TEdit-Component but it
should work on oll other components with OnKeyPress-Event.
Create a new Component from TEdit with a new property:
EnterNextCtrl: Boolean
If this is TRUE (standard), the Focus will jump to next Control if the user press
ENTER. If it is FALSE it will show standard-behaviour (beep). This functionality is
included in OnKeyPress-Event. There the component sends the Message "WM_NextDLGCTL"
to the parent Form. It's important to get the ParentForm, because the TMyEdit need
not included on TForm directly but maybe on a TPanel. So you have to send the
message directly to the parent-FORM (and not to PARENT, which may be a TPanel).
It should be possible to use this way for other components which have a
OnKeyPress-Event.
1 type2 TMyEdit = class(TEdit)
3 private4 FEnterNextCtrl: Boolean;
5 protected6 procedure KeyPress(var Key: Char); override;
7 published8 property EnterNextCtrl: Boolean read FEnterNextCtrl write FEnterNextCtrl;
9 constructor Create(AOwner: TComponent); override;
10 end;
11 12 constructor TMyEdit.Create(AOwner: TComponent);
13 begin14 inherited;
15 FEnterNextCtrl := TRUE;
16 end;
17 18 procedure TMyEdit.KeyPress(var Key: Char);
19 var20 ParentForm: TCustomForm;
21 begin22 inherited;
23 if key = #13 then24 begin25 Key := #0;
26 27 //Get the parent-Form.28 ParentForm := GetParentForm(self);
29 30 if FEnterNextCtrl = TRUE {//Jump to next Control or beep }then31 parentform.Perform(WM_NextDLGCTL, 0, 0)
32 else33 messageBeep(0);
34 end;
35 end;