Recent

Author Topic: [SOLVED] Making Lazarus dbGrid act like Delphi on data entry  (Read 3128 times)

eshjim

  • New Member
  • *
  • Posts: 27
[SOLVED] Making Lazarus dbGrid act like Delphi on data entry
« on: August 06, 2016, 08:19:02 pm »
When appending a new record to the end of a dbGrid, in Delphi, after completing the last field, a tab automatically inserts a new blank record with the focus on the first field of the record. This enables fast data entry. However, in Lazarus, a tab after the last field is completed moves the cursor to the next object in the tab order. In the grid, the focus stays on the newly inserted record. Is there any way of making Lazarus emulate Delphi either at design time or in code?
« Last Edit: August 06, 2016, 08:30:17 pm by eshjim »

eshjim

  • New Member
  • *
  • Posts: 27
Re: [SOLVED] Making Lazarus dbGrid act like Delphi on data entry
« Reply #1 on: August 06, 2016, 08:49:31 pm »
After a little more playing about, I solved it  :)

For any other Lazarus newbies that may be interested, this is the code:

Code: Pascal  [Select]
  1. procedure TfmMain.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3.  
  4. begin
  5.   if Key = VK_TAB then
  6.     if DBGrid1.SelectedIndex = 2 then // grid has 3 fields
  7.         begin
  8.           dmData.SQLDetail.Append;
  9.           DBGrid1.SelectedIndex := 0;
  10.           Key := 0; // prevents tab from operating and taking cursor to
  11.                     // second field of new record
  12.         end;
  13. end;

My problem previously was that I was over-coding, which just goes to show that sometimes less is more!!

shobits1

  • Sr. Member
  • ****
  • Posts: 278
  • .
Re: [SOLVED] Making Lazarus dbGrid act like Delphi on data entry
« Reply #2 on: August 07, 2016, 12:49:02 am »
I tracked down the problem and I think it's small bug, experienced developer, please confirm:

lazarus\lcl\dbgrids.pas ,, around line: 2369
Code: Pascal  [Select]
  1. if (not (ssShift in Shift)) and (Row>=GetLastVisibleRow) and
  2.    (DeltaRow>0) and (Col=GetLastVisibleColumn) and
  3.    (FDatalink.Editing or not GridCanModify) then begin
  4.   {$ifdef dbgGrid}DebugLnExit('%s.KeyDown Exit: Tab: not shift',[ClassName]);{$endif}
  5.   exit;
  6. end;
  7.  

should be changed to :
Code: Pascal  [Select]
  1. if (not (ssShift in Shift)) and (Row>=GetLastVisibleRow) and
  2.    (DeltaRow>0) and (Col=GetLastVisibleColumn) and
  3.    (not GridCanModify) then begin
  4.   {$ifdef dbgGrid}DebugLnExit('%s.KeyDown Exit: Tab: not shift',[ClassName]);{$endif}
  5.   exit;
  6. end;
  7.  

as you can see,, it checks if row/record is in edit mode and since new row/record is always in edit mode, therefor it's bug.