Recent

Author Topic: TStringGrid Question  (Read 382 times)

J-G

  • Hero Member
  • *****
  • Posts: 1055
TStringGrid Question
« on: June 06, 2026, 12:32:57 pm »
I find the TStringGrid a very powerful tool but it's giving me some grief at the moment  :(

I'm using it to store a history of inputs to my Triangle program so that I can go back to previous solutions by simply [clicking] on the [row] to re-enter the original values and then either re-solve or modify as required. The option to [Delete] has also been incorporated - I just click on a paricular column and test for that to determine 'select' or 'delete' - naturally I check that 'Delete' is intentional!

Early on I found that the procedure was being activated whilst the 'History' was being read and I could get around that with a simple 'If Reading' flag. It was also being triggered when the grid was hidden - again that needed a flag gleaned from the state of the activation button.

All seemed to be working well - ie. clicking on a row inserted the data correctly and clicking on the particular column offered the [Delete] option BUT  after a 'delete' and without further mouse activity near the Grid the [Delete?] option repeatedly appears.

In addition, the [Hint] - which simple reminds the user which column to click for a 'Delete' - doesn't show when a mouse pointer is hovering  -  I HAVE made [ShowHint] True !  ::)

What I cannot understand is why the 'OnSelectCell' event is triggered when the Grid isn't visible - and as such there cannot be a 'select' event.

During debugging I've found that at the start of the program the proc is called 5 times before the form is even created ?????

There must be something that I'm missing - but it's beyond my feable brain !

Here is the Proc :-
Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.HistorySelectCell(Sender: TObject; aCol, aRow: Integer; var CanSelect: Boolean);
  2. var
  3.   S : TStringArray;
  4.   R,Q : Word;
  5.  
  6. //  Select or Delete  the row dependinig upon which column is [clicked]
  7.  
  8. begin
  9.   if Reading or (ShowHistory.Caption[1] = 'S') then Exit
  10.   else
  11.     begin
  12.       If aCol = 3 then
  13.         begin
  14.           if MessageDlg('', 'DELETE this line ?', mtConfirmation,
  15.              [mbYes, mbNo],0) = mrYes
  16.           then
  17.             begin
  18.               R := aRow;
  19.               While R < History.RowCount-1 do
  20.                 begin
  21.                   Q := R+1;
  22.                   with History do
  23.                     begin
  24.                       Cells[0,R] := Cells[0,Q];
  25.                       Cells[1,R] := Cells[1,Q];
  26.                       Cells[2,R] := Cells[2,Q];
  27.                       Cells[3,R] := Cells[3,Q];
  28.                       Cells[4,R] := Cells[4,Q];
  29.                       Cells[5,R] := Cells[5,Q];
  30.                       Cells[6,R] := Cells[6,Q];
  31.                     end;
  32.                   inc(R);
  33.                 end;
  34.               History.RowCount := History.RowCount-2;
  35.               MaxHist := History.RowCount;
  36.             end;
  37.         end
  38.       else
  39.         begin
  40.           with TriangleSolution do
  41.             begin
  42.               S  :=History.Cells[0,aRow].Split(['·']);
  43.               Val_A.Caption  := S[0]; Val_A1.Caption := S[1];
  44.               S  :=History.Cells[1,aRow].Split(['·']);
  45.               Val_B.Caption  := S[0]; Val_B1.Caption := S[1];
  46.               S  :=History.Cells[2,aRow].Split(['·']);
  47.               Val_C.Caption  := S[0]; Val_C1.Caption := S[1];
  48.               S  :=History.Cells[4,aRow].Split(['·']);
  49.               Val_Alpha.Caption  := S[0]; Val_Alpha1.Caption := S[1];
  50.               S  :=History.Cells[5,aRow].Split(['·']);
  51.               Val_Beta.Caption  := S[0]; Val_Beta1.Caption := S[1];
  52.               S  :=History.Cells[6,aRow].Split(['·']);
  53.               Val_Gamma.Caption  := S[0]; Val_Gamma1.Caption := S[1];
  54.             end;
  55.           HistSelect := true;
  56.         end;
  57.   end;
  58. end;
  59.  


I know I could use a loop for the delete part but there's only 7 items.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Xenno

  • Full Member
  • ***
  • Posts: 111
    • BS Programs
Re: TStringGrid Question
« Reply #1 on: June 06, 2026, 01:19:01 pm »
Probably you need to make use of BeginUpdate and EndUpdate of the grid.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

J-G

  • Hero Member
  • *****
  • Posts: 1055
Re: TStringGrid Question
« Reply #2 on: June 06, 2026, 01:31:47 pm »
Probably you need to make use of BeginUpdate and EndUpdate of the grid.

I don't see any reference to either of those options in the [Properties] or [Events] of TStringGrid.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Xenno

  • Full Member
  • ***
  • Posts: 111
    • BS Programs
Re: TStringGrid Question
« Reply #3 on: June 06, 2026, 01:44:31 pm »
https://lazarus-ccr.sourceforge.io/docs/lcl/grids/tcustomgrid.beginupdate.html

Example:
Code: Pascal  [Select][+][-]
  1.   StringGrid1.BeginUpdate;
  2.   try
  3.     // busy work involving the grid
  4.   finally
  5.     StringGrid1.EndUpdate;
  6.   end;
       
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

J-G

  • Hero Member
  • *****
  • Posts: 1055
Re: TStringGrid Question
« Reply #4 on: June 06, 2026, 02:38:22 pm »
Example:
Code: Pascal  [Select][+][-]
  1.   StringGrid1.BeginUpdate;
  2.   try
  3.     // busy work involving the grid
  4.   finally
  5.     StringGrid1.EndUpdate;
  6.   end;
     
Thanks for your input @Xenno but I'm still at a loss as to how this might help  :-[

I presume that I would have to use something like :
Code: Pascal  [Select][+][-]
  1. procedure TTriangleSolution.HistorySelectCell(Sender: TObject; aCol, aRow: Integer; var CanSelect: Boolean);
  2. var
  3.   S : TStringArray;
  4.   R,Q : Word;
  5. begin
  6.   History.BeginUpdate;
  7.    try
  8.     // existing code for select or Delete
  9.  finally
  10.    History.EndUpdate;
  11. end
  12.  

but that doesn't seem to address the fact that 'HistorySelectCell' is being triggered when the TStringGrid isn't even visible  ?
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Xenno

  • Full Member
  • ***
  • Posts: 111
    • BS Programs
Re: TStringGrid Question
« Reply #5 on: June 06, 2026, 03:06:38 pm »
I am sorry it does not help.

May you share a screenshot or two?
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

J-G

  • Hero Member
  • *****
  • Posts: 1055
Re: TStringGrid Question
« Reply #6 on: June 06, 2026, 04:03:48 pm »
I am sorry it does not help.

May you share a screenshot or two?

Happy to  - whether it helps is debatable  :)

I'll also attach a complete [Published] project.

One issue is that I use two screens and the TStringGrid is shown on the second screen - but you can move the whole window to the left on a single screen.

The 'Hint' has appeared on one occasion but did not re-appear when the mouse pointer hovered again.

After a [Delete] any movement of the mouse on the second screen triggers the 'SelectCell' proc even though the pointer is nowhere near the grid. Prior to a [Delete] the cells are all white, though the selected row is highlighed as demanded by goRowHighlight. the blue you see in the Screen Grab appears to be random though I suspect that some mouse movement is responsible;  it does not show until after a [Delete] has been requested (by clicking on the 'TOS' column).

It is also triggered at start-up 6 times - ie. in debug mode with a break-point set at line 5808, I have to hit [ f9 ] six times before the main screen appears.  ????


FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Xenno

  • Full Member
  • ***
  • Posts: 111
    • BS Programs
Re: TStringGrid Question
« Reply #7 on: June 06, 2026, 06:16:09 pm »
Thank you for the screenschot. Now, I think I can see what you want to achieve. IIRC, OnSelectCell event is where we decide the cell can be selected or not. It shouldn't be the place for much work especially altering the grid. To react after a cell is selected, use OnClick or OnSetEditText event instead.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

J-G

  • Hero Member
  • *****
  • Posts: 1055
Re: TStringGrid Question
« Reply #8 on: June 06, 2026, 06:39:01 pm »
Hmmmm. . .

I have looked at OnClick and OnButtonClick but neither of them have the aRow & aCol attributes so (I presume) won't allow me to determine which Row or Column has been [Clicked].

I do see that OnSetEditText does have them so it may be worth a look - though I am not intending to edit the cells at all, just glean data from them - or Delete the row  -  It may well be that I'm over-thinking the issue :)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Xenno

  • Full Member
  • ***
  • Posts: 111
    • BS Programs
Re: TStringGrid Question
« Reply #9 on: June 06, 2026, 06:43:04 pm »
You can read the current (coordinate of) selected cell is by reading the grid.Row and grid.Col.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

J-G

  • Hero Member
  • *****
  • Posts: 1055
Re: TStringGrid Question
« Reply #10 on: June 06, 2026, 06:50:27 pm »
You can read the current (coordinate of) selected cell is by reading the grid.Row and grid.Col.

That is where my presumption failed   :D  - 

I've found that SetEditText doesn't take any action (that I need) on a click - it simply redies the cell for Editing (as I would anticipate).

I'll now try the OnClick again with - I'm guessing - History.Row or History.Col which is what I suspect you mean by grid.Row . . . .   'History' being the name of the TStringGrid.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

J-G

  • Hero Member
  • *****
  • Posts: 1055
Re: TStringGrid Question
« Reply #11 on: June 06, 2026, 07:34:58 pm »
! EUREKA !

All is clear - no more false triggering of 'Events'  -  and the majority of the 'Flags' that I had incorporated to circumvent the behaviour have been eliminated !

Thanks for your steadfast assistance @Xenno  -  to be honest I ought to have seen the means by which I could access aRow and aCol  :-[  without your input - I was already accessing History.RowCount  ::)   to make sure that I didn't fall into a range-check problem!

 
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Xenno

  • Full Member
  • ***
  • Posts: 111
    • BS Programs
Re: TStringGrid Question
« Reply #12 on: June 06, 2026, 07:40:18 pm »
I'm glad you've made it. Please, apology for not giving right direction earlier cause of my slow understanding.
Lazarus 4.0, Windows 10, https://www.youtube.com/@bsprograms

J-G

  • Hero Member
  • *****
  • Posts: 1055
Re: TStringGrid Question
« Reply #13 on: June 06, 2026, 08:05:52 pm »
I'm glad you've made it. Please, apology for not giving right direction earlier cause of my slow understanding.

Absolutely NO apology needed !  -  getting into the mindset of someone you've never spoken to is a daugnting task;  the more so when that 'someone' doesn't even know what questions to ask  :D
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018