From: koterski@NOSPAMgte.net (Steve Koterski)
Subject: Re: Enter as Tab
Date: 13 Jan 2000 00:00:00 GMT
Message-ID: <3880f5fa.2349618@news.gte.net>
Distribution: world
Content-Transfer-Encoding: 7bit
References: <387E20D1.89AC9895@codetel.net.do>
Content-Type: text/plain; charset=us-ascii
X-Abuse-Info: Otherwise we will be unable to process your complaint properly
X-Complaints-To: abuse@gte.net
X-Trace: +Li/tVJTJkGTmlTw+qEWa2uaRFbcdshoxft69LiM7zJibipH4o6pF/houdhD6qArTmqxUsCev9NM!hXgOy+omQzmYDbH90383u1tXSj+DTLkAr6Vnch4Hqk1Dh0T4hpsi3azZkA==
Organization: GTE Internet Solutions
MIME-Version: 1.0
NNTP-Posting-Date: Thu, 13 Jan 2000 15:58:22 GMT
Reply-To: koterski@NOSPAMgte.net
Newsgroups: comp.lang.pascal.delphi.databases
On Thu, 13 Jan 2000 15:00:57 GMT, Gregorio Mariano
wrote:
> How to make the focus go to the next control on the tab order when i
>press Enter ?
Using Tab to navigate between child controls in a window is a Windows
convention. However, it is possible to override this default behavior. You
can do this in Delphi once, at the TForm level, obviating the need to write
a handler for each control.
First, you have to force all keystrokes to be reviewed by the containing
TForm prior to them being passed on to the individual child controls. This
is done by setting the KeyPreview property of the TForm to True.
The last step involves evaluating the keystrokes seen by the TForm and
reacting to only the Enter key (#13 or the Windows constant VK_RETURN). If
the Enter key is encountered, call the Perform method to move to the next
child control with a call to the form's Perform method and then nullify the
keystroke. This is done in an OnKeyPress event procedure for the TForm:
procedure Form1.FormKeyPress(Sender: TObject; var Key: Char);
begin
if (Ord(Key) = VK_RETURN { the Enter key }) then begin
{ Always nullify keystrokes handled }
Key := #0;
{ Signal to move focus to next control }
Perform(WM_NEXTDLGCTL, 0, 0);
end;
end;
A value of zero for the second parameter of Perform (the WParam parameter)
moves focus to the next control in the tab-order. A value of negative one
moves focus to the prior control in the tab-order. So you could trap for
two keys and either move forward or back in one routine depending on the
key pressed. Below, Enter moves to the next control and UpArrow to the
prior control.
procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key in [VK_RETURN, VK_UP]) then begin
if (Key = VK_RETURN { the Enter key }) then
{ Signal to move focus to next control }
Perform(WM_NEXTDLGCTL, 0, 0)
else { the UpArrow key }
{ Signal to move focus to prior control }
Perform(WM_NEXTDLGCTL, -1, 0);
{ Always nullify keystrokes handled }
Key := 0;
end;
end;
Note that because the UpArrow key does not trigger an OnKeyPress event, the
OnKeyUp event must be used instead. In hanlders for OnKeyUp, the Key
parameter is of type Word, not Char as in OnKeyPress. Handle the difference
in data types as indicated.
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski "Health nuts are going to feel stupid someday,
Felton, CA lying in hospitals dying of nothing."
-- Redd Foxx