Recent

Author Topic: [Solved] TStringGrid with AutoAdvance:= aaDown.  (Read 5376 times)

Avishai

  • Hero Member
  • *****
  • Posts: 1021
[Solved] TStringGrid with AutoAdvance:= aaDown.
« on: August 01, 2011, 05:53:28 pm »
I have a TStringGrid with AutoAdvance:= aaDown.  When the user is in the bottom row of a Column that is not the last column, What I want is for Tab, Return or Down-Key to go to the Top Row of the next Column.  I have code that does this, but it only works if the user does not type anything in the bottom cell.  If they do, the focus goes to the next control instead of advancing the way I described. The code does execute, but it seems to be ignored.  Any ideas?

procedure TForm1.sgAvarKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  with Sender as TstringGrid do begin
    if (Col < ColCount-1) and (Row = RowCount-1) then begin
      if Key in [VK_Tab,VK_RETURN,VK_DOWN] then begin
        Col:= Col+1;
        Row:= FixedRows;
        Key:= 0;
      end;
    end;
  end;
end;
« Last Edit: August 08, 2011, 02:53:47 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: TStringGrid with AutoAdvance:= aaDown.
« Reply #1 on: August 08, 2011, 02:53:12 pm »
YES!  I finally got it!

procedure aaDownWrap(Sender: TObject; var Key: Word);
var
  ParentForm: TCustomForm;
  Flag: Boolean;
  I: Integer;
begin
  with Sender as TStringGrid do begin
    if Key <> VK_Tab then exit;
    Flag:= False;
    if (Row = RowCount-1)and(Key = VK_Tab) then begin
      Flag:= (Col = ColCount-1)and(Row = RowCount-1);
      Col:= Col+1;
      Row:= FixedRows;
      Key:= 0;
    end;
    ParentForm:= GetParentForm(Sender as TStringGrid);
    if Flag then begin
      Row:= FixedRows;
      Col:= FixedCols;
      I:= TabOrder+1;
      if I = ParentForm.ControlCount then
        I:= 0;
      ParentForm.FocusControl(ParentForm.Controls/[I/] as TWinControl);
    end else
      if Not Flag then
        SetFocus;
  end;
end;

procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  with Sender as TStringGrid do begin
    case AutoAdvance of
      aaDown: aaDownWrap(Sender, Key);
    end;
  end;
end;
« Last Edit: August 08, 2011, 03:08:18 pm by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: [Solved] TStringGrid with AutoAdvance:= aaDown.
« Reply #2 on: August 08, 2011, 05:51:09 pm »
This code is only partly complete.  I still have work to do.  Shift-Tab...
Lazarus Trunk / fpc 2.6.2 / Win32

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [Solved] TStringGrid with AutoAdvance:= aaDown.
« Reply #3 on: August 08, 2011, 06:01:05 pm »
Keep at it, Avishai - just wishing you good luck. Can be very frustrated to figure this kind of thing out..
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: [Solved] TStringGrid with AutoAdvance:= aaDown.
« Reply #4 on: August 08, 2011, 06:06:49 pm »
Yes, it can be very frustrating at times, but you almost always learn new things in the process.  Things you might not find any other way.  For example,

 ParentForm:= GetParentForm(Sender as TStringGrid);

I wouldn't have found this otherwise and it allows me to do a lot of things in a very generic way.
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018