First of I would like to say hi to everyone as I am new to this forum.

My problem is this. Can I some how use the [ENTER] key instead of the [TAB] key to skip from field to field? Any help would be greatly apreciated.

Thank you.

Recommended Answers

All 4 Replies

place this code in the first edit onkeydown event

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
case key of
13:edit2.SetFocus;
end;
end;


best regards,

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:

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

Thanks both of you for your help I will try it out and see if it preforms the way I want it to.

also you can try to hook up the windows key messages. in this way you can handle all the keys include TAB.

best regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.