Board index » delphi » Using the Return Key instead of Tab

Using the Return Key instead of Tab

Does anyone know how to use the Return key to move between text boxes on a
form at runtime, instead of using the Tab key. I write programs which require
a lot of fields on different forms, I would like to be able to let users use
the Return key to navigate the form, and I would like to be able to do this
without writing too much code.

Thanks.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading

 

Re:Using the Return Key instead of Tab


This is a pretty common question... :-)

The easiest way is to write a Form.OnKeyPress handler for each form;
you can cut-n-paste the part of the code between the begin and
end on each form you want to use it, and the Object Inspector
will generate the shell.

First, set KeyPreview on the form to True. Change to the events
page and double-click the OnKeyPress. Delphi will generate all
of the code for the method, and place your cursor between the
begin..end statements. Insert this code:

    if Key = #13 then begin
        Key := #0;
        Perform(WM_NEXTDLGCTL, 0, 0);
    end;

That's it!

HTH

Ken
--
Ken White
kwh...@westelcom.com

Clipper Functions for Delphi
http://members.aol.com/clipfunc/

Quote
a_adaram...@my-dejanews.com wrote:

> Does anyone know how to use the Return key to move between text boxes on a
> form at runtime, instead of using the Tab key. I write programs which require
> a lot of fields on different forms, I would like to be able to let users use
> the Return key to navigate the form, and I would like to be able to do this
> without writing too much code.

> Thanks.

> -----== Posted via Deja News, The Leader in Internet Discussion ==-----
> http://www.dejanews.com/   Now offering spam-free web-based newsreading

Re:Using the Return Key instead of Tab


  For most components you can use the OnKeyPress event to trap the Enter
key and then do what you want with it.  The following is an example of how
to trap the Enter key in a TEdit and sent the focus to the next control in
the tab order.  It also kills the annoying beep.  You can use the same
event for more than one control on a form by setting it using the object
inspector.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then  {#13 is the enter key}
  begin
    Key := #0;  {Kill the key so there is no beep}
    PostMessage(Handle, wm_NextDlgCtl, 0, 0);  {Send focus to the next
control}
  end;
end;

If you wanted this to work for all controls on a form (say 4 or 5 TEdits)
then you would set the forms KeyPreview property to true for the form and
then put the same code in the forms OnKeyPress event.  I.e.:

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    PostMessage(Handle, wm_NextDlgCtl, 0, 0);
  end;
end;

Hope this helps!
--

Rodney E Geraghty
GERA-Tech
Ottawa, Canada
gera...@ibm.net

a_adaram...@my-dejanews.com wrote in article
<6m9771$d4...@nnrp1.dejanews.com>...

Quote
> Does anyone know how to use the Return key to move between text boxes on
a
> form at runtime, instead of using the Tab key. I write programs which
require
> a lot of fields on different forms, I would like to be able to let users
use
> the Return key to navigate the form, and I would like to be able to do
this
> without writing too much code.

> Thanks.

> -----== Posted via Deja News, The Leader in Internet Discussion ==-----
> http://www.dejanews.com/   Now offering spam-free web-based newsreading

Other Threads