Set Enter Key to work like Tab Key in Delphi Application

In Delphi Applications, we use Tab Key to move focus from one control to another control. But some user prefer to press Enter key to move focus from one control to another control for speed data entry work. As Delphi does not provide such in-build facility or any in build property to set Enter Key as Tab. So here I will explain how to set Enter key to behave like Tab, and Shift+Enter like Shift+Tab. But before, I assume that there is no default button on the form. When a form contains a button and whose Default property is set to True then pressing Enter at run-time executes any code contained in the button's OnClick event handler. 

So here we will check with different examples for setting Enter key as tab in Edit controls, grid controls and globally on a form. 

For all Form controls that ability to focus

We can set this functionality for controls like EditBox, Memo, Combo, Listbox,  RadioButton, Checkbox etc and also other controls that have ability to focus.

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
// on enter next control select function //
if (Key = Char(VK_RETURN)) then
begin
If HiWord(GetKeyState(VK_SHIFT)) <> 0 then
SelectNext(ActiveControl, True, True)
Else
SelectNext(ActiveControl, True, True);
Key := Char(0);
end;
end;

For VCL DBGrid

If you want to have similar Enter (Shift+Enter) processing in DBGrid:

procedure TForm1.DBGrid1KeyPress (Sender: TObject; var Key: Char) ;
begin
If Key = #13 Then 
Begin
If HiWord(GetKeyState(VK_SHIFT)) <> 0 then 
begin
with (Sender as TDBGrid) do
begin
if selectedindex > 0 then
selectedindex := selectedindex - 1
else 
begin
DataSource.DataSet.Prior;
selectedindex := fieldcount - 1;
end;
end;
end 
else 
begin
with (Sender as TDBGrid) do
begin
if selectedindex < (fieldcount - 1) then
selectedindex := selectedindex + 1
else 
begin
DataSource.DataSet.Next;
selectedindex := 0;
end;
end;
end;
Key := #0;
end;
end;

For DevExpress DB Grid

When we use cxGrid then we have to just set following property to True.  

cxGrid1DBTableView1.OptionsBehavior.GoToNextCellOnEnter := True;

For TMS DbGrid

When we use TMS TDBAdvGrid then we have to set following properties.

AdvDBGrid.Navigation.AdvanceOnEnter := True;
AdvDBGrid.Navigation.AdvanceOnEnterLoop := True;

For Woll2Woll DBGrid

For TWWDbGrid we have to smart key mapping feature. 

For EHLib DbGrid

When we use TdbGridEh then we have to set 

GridEh.Options.EnterToNextCell := True;

Comments

Post a Comment

Popular posts from this blog

ShellExecute in Delphi

How to send Email in Delphi?

Drawing Shapes in Delphi