Lazarus

Programming => General => Topic started by: Deepaak on June 20, 2021, 11:05:39 am

Title: [SOLVED] Stringgrid AutoExpand without goEditing
Post by: Deepaak on June 20, 2021, 11:05:39 am
Hi to all,

I have a stringgrid with only one column and 5 rows. Normally when pressing VK_DOWN(ArrowKey) the focus is transfered to next row. But when the focus is transfered to last row, how to handle this situation, so that when the focus is in last row then pressing the VK_DOWN key again in last row transfers the focus to first row of the stringgrid.



Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.    caption := StringGrid1.RowCount.ToString + ' : ' +  Stringgrid1.Row.ToString;
  5.    if (key = VK_DOWN) then  begin
  6.      with(sender as tstringgrid) do begin
  7.          if (Row = (RowCount - 1)) Then Begin
  8.               Row := 0
  9.          end;
  10.      end;
  11.   end;
  12. end;      
Title: Re: Stringgrid AutoExpand without goEditing
Post by: wp on June 20, 2021, 11:29:07 am
Disable automatic adding of a new row when the selected cell is in the last row (Option goAutoAddRows). And in your OnKeyDown handler disable further processing of the pressed key by setting Key := 0 in the innermost "if" condition:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.    caption := StringGrid1.RowCount.ToString + ' : ' +  Stringgrid1.Row.ToString;
  5.    if (key = VK_DOWN) then  begin
  6.      with(sender as tstringgrid) do begin
  7.          if (Row = (RowCount - 1)) Then Begin
  8.               Row := 0;
  9.               Key := 0;
  10.          end;
  11.      end;
  12.   end;
  13. end;
Title: Re: Stringgrid AutoExpand without goEditing
Post by: Deepaak on June 20, 2021, 11:35:54 am
Disable automatic adding of a new row when the selected cell is in the last row (Option goAutoAddRows). And in your OnKeyDown handler disable further processing of the pressed key by setting Key := 0 in the innermost "if" condition:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. begin
  4.    caption := StringGrid1.RowCount.ToString + ' : ' +  Stringgrid1.Row.ToString;
  5.    if (key = VK_DOWN) then  begin
  6.      with(sender as tstringgrid) do begin
  7.          if (Row = (RowCount - 1)) Then Begin
  8.               Row := 0;
  9.               Key := 0;
  10.          end;
  11.      end;
  12.   end;
  13. end;

Awesome, worked like a charm  :) Thank you
TinyPortal © 2005-2018