Recent

Author Topic: String Grid move to cell  (Read 427 times)

bowlofpetunias

  • Newbie
  • Posts: 3
String Grid move to cell
« on: April 26, 2025, 03:21:04 pm »
what I would like to achieve is to have the cursor in the cell with the invalid data so the user can amend the information

I am unable to get the cursor to move into the requested cell

Thank you for your assistance

Code: Pascal  [Select][+][-]
  1. unit Unit2;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids, StdCtrls,
  9.   Windows;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     StaticText1: TStaticText;
  17.     StringGrid1: TStringGrid;
  18.     procedure StringGrid1EditingDone(Sender: TObject);
  19.     Procedure GoToColRow(col, row : integer);
  20.  
  21.   private
  22.  
  23.   public
  24.  
  25.   end;
  26.  
  27. var
  28.   Form1: TForm1;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm1 }
  35.  
  36. procedure TForm1.StringGrid1EditingDone(Sender: TObject);
  37. var
  38.   bng : real;
  39.   acol, arow, code : integer;
  40.  
  41. begin
  42.   StaticText1.Caption:='';
  43.   acol := stringgrid1.Col;
  44.   arow := stringgrid1.Row;
  45.  
  46.   Val(stringgrid1.Cells[acol, arow], bng, code);
  47.   if bng > 360 then
  48.   begin
  49.     windows.beep(500,200);
  50.     StaticText1.Caption:=' ... illegal value';
  51.     GoToColRow(acol, arow);
  52.   end;
  53. end;
  54.  
  55.  
  56.  
  57. Procedure TForm1.GoToColRow(col, row : integer);
  58.  
  59. begin
  60.   //stringgrid1.Options:=stringgrid1.Options - [goediting];
  61.   stringgrid1.Col:=col;
  62.   stringgrid1.Row:=row;
  63.  
  64.   col := stringgrid1.Col;
  65.   row := stringgrid1.Row;
  66.  
  67.   //stringgrid1.Options:=stringgrid1.Options + [goediting];
  68.   //statictext1.Caption:='';
  69. end;
  70.  
  71.  
  72.  
  73.  
  74. end.      
  75.  

Handoko

  • Hero Member
  • *****
  • Posts: 5425
  • My goal: build my own game engine using Lazarus
Re: String Grid move to cell
« Reply #1 on: April 26, 2025, 03:59:46 pm »
Hello bowlofpetunias,
Welcome to the forum.

I believe this demo is what you need:
https://forum.lazarus.freepascal.org/index.php/topic,37286.msg250687.html#msg250687

If you want to learn other simple tricks, I recommend this link:
https://wiki.freepascal.org/Portal:HowTo_Demos

bowlofpetunias

  • Newbie
  • Posts: 3
Re: String Grid move to cell
« Reply #2 on: April 27, 2025, 07:04:15 am »
Thanks Handoko

that works well.
Seems to me to be a complicated way of accomplishing something that that should be fairly simple.

I tried the following, which googling said was supposed to work but did not

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1EditingDone(Sender: TObject);
  2. begin
  3.   stringgrid1.Row:=1;
  4.   stringgrid1.Col:=3;
  5.   stringgrid1.SetFocus;
  6. end;    
  7.  

Handoko

  • Hero Member
  • *****
  • Posts: 5425
  • My goal: build my own game engine using Lazarus
Re: String Grid move to cell
« Reply #3 on: April 27, 2025, 07:55:37 am »
  stringgrid1.Row:=1;
  stringgrid1.Col:=3;
  stringgrid1.SetFocus;



Actually these 3 lines above worked as they should be. But you forgot to think, that after the stringgrid process the OnEditingDone event, the stringgrid will also reposition the active cell immediately depends on user input. For example user done the editing by pressing Enter or arrow down key. So your code for moving the cell to the desired location should be process after a short delay after OnEditingDone event. That's why I use a TTimer.

bowlofpetunias

  • Newbie
  • Posts: 3
Re: String Grid move to cell
« Reply #4 on: April 27, 2025, 01:05:41 pm »
Ah ... yes, thanks for the explanation, much appreciated




 

TinyPortal © 2005-2018