2009. szeptember 4., péntek

How to make the Enter key act as the Tab key while inside a TDBGrid


Problem/Question/Abstract:

How to make the Enter key act as the Tab key while inside a TDBGrid

Answer:

This code also includes the processing of the [Enter] key for the entire application - including fields, etc. The grid part is handled in the ELSE portion of the code. The provided code does not mimic the behavior of the key stepping down to the next record when it reaches the last column in the grid - it moves back to the first column.

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
{This is the event handler for the form's OnKeyPress event!}
{You should also set the Form's KeyPreview property to True}
begin
  if Key = #13 then { if it's an Enter key }
    if not (ActiveControl is TDBGrid) then { if not on a TDBGrid }
    begin
      Key := #0; { eat Enter key }
      Perform(WM_NEXTDLGCTL, 0, 0); { move to next control }
    end
    else if (ActiveControl is TDBGrid) then { if it is a TDBGrid }
      with TDBGrid(ActiveControl) do
        if selectedindex < (fieldcount - 1) then { increment the field }
          selectedindex := selectedindex + 1
        else
          selectedindex := 0;
end;

Nincsenek megjegyzések:

Megjegyzés küldése