Recent

Author Topic: [SOLVED] Stringgrid AutoExpand without goEditing  (Read 1881 times)

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
[SOLVED] Stringgrid AutoExpand without goEditing
« 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;      
« Last Edit: June 20, 2021, 11:36:16 am by Deepaak »
Holiday season is online now. :-)

wp

  • Hero Member
  • *****
  • Posts: 11858
Re: Stringgrid AutoExpand without goEditing
« Reply #1 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;

Deepaak

  • Sr. Member
  • ****
  • Posts: 454
Re: Stringgrid AutoExpand without goEditing
« Reply #2 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
Holiday season is online now. :-)

 

TinyPortal © 2005-2018