Recent

Author Topic: Unselect all rows in stringgrid  (Read 2091 times)

CM630

  • Hero Member
  • *****
  • Posts: 1312
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Unselect all rows in stringgrid
« Reply #15 on: February 21, 2025, 07:47:59 am »
Did you solve your issue?
I remember that for some reasons I was not very happy with TStringGrid.
Maybe you can take a look at this thread: https://forum.lazarus.freepascal.org/index.php?topic=66162.0
Лазар 4,0RC2 32 bit (sometimes 64 bit); FPC3,2,2

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: Unselect all rows in stringgrid
« Reply #16 on: February 24, 2025, 08:30:25 pm »
Sorry @CM630, I did not see that you had replied here.

Indeed, TStringGrid is very fiddly, but I am working around the issues, I am too invested in it now to swap to something else and learning lots with my patches!

If you are interested, currently stuck on this: select a row without any cell in that row becoming active for edits.

I want the user to select a row, then press various keys on the keyboard to do stuff relating to the row. But the key presses tinstantly activate the editor and the characters appear there.

What I need is: user clicks a row to select it, then optionally press a selection of keys, then click again on a cell in the row to start edit mode.

Tricky. Any ideas?

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: Unselect all rows in stringgrid
« Reply #17 on: February 24, 2025, 11:34:05 pm »
Aha! In case anyone is interested (or want to criticise and offer a more elegant solution):

Code: Pascal  [Select][+][-]
  1. var
  2.   CurrentRow: Integer;
  3.  
  4. procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer; var CanSelect: Boolean);
  5. begin
  6.   if CurrentRow <> aRow then
  7.     StringGrid1.Options := StringGrid1.Options - [goEditing]
  8.   else
  9.     StringGrid1.Options := StringGrid1.Options + [goEditing];
  10.   CurrentRow := aRow;
  11. end;
  12.  

wp

  • Hero Member
  • *****
  • Posts: 12761
Re: Unselect all rows in stringgrid
« Reply #18 on: February 25, 2025, 12:59:53 am »
If you are interested, currently stuck on this: select a row without any cell in that row becoming active for edits.
The official way to prevent editing of specific cells is to handle the OnSelectEditor event and set the Editor argument to nil. The following code forbids editing of all cells in row 2:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer;
  2.   var Editor: TWinControl);
  3. begin
  4.   if aRow = 2 then Editor := nil;
  5. end;
Activate goRowSelect to mark the entire row as selected. You can still catch key presses in the OnKey* events of the grid event if a cell is disabled for editing as shown above.

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: Unselect all rows in stringgrid
« Reply #19 on: February 25, 2025, 12:49:30 pm »
Quote from: wp
handle the OnSelectEditor event and set the Editor argument to nil

Thanks @wp, unfortunately that is not working for me. I check if the clicked row is a new row then set it to nil if it is, but typing into the keyboard always edits the cell.

Code: Pascal  [Select][+][-]
  1. var
  2.   CurrentRow: Integer;
  3. procedure TForm1.StringGrid1SelectEditor(Sender: TObject; aCol, aRow: Integer; var Editor: TWinControl);
  4. var
  5.   IsNewRow: Boolean;
  6. begin
  7.   IsNewRow := aRow <> CurrentRow;
  8.   if IsNewRow then Editor := nil;
  9.   CurrentRow := aRow;
  10. end;
  11.  

Can you please look at the attached project which also includes logging to confirm the logic?

wp

  • Hero Member
  • *****
  • Posts: 12761
Re: Unselect all rows in stringgrid
« Reply #20 on: February 25, 2025, 06:08:48 pm »
Your code is better suited to your requirement.

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: Unselect all rows in stringgrid
« Reply #21 on: February 26, 2025, 12:57:01 am »
Thanks @wp, it is always reassuring to get your confirmation.

Last issue with the grid and I think that I am done:

When the user edits a cell and then presses the enter key then TStringGrid.OnValidateEntry happens correctly.

But if user edits cell then clicks another cell, events fire as OnMouseDown then SelectCell then OnValidateEntry.

Is it possible to change the order and trigger the OnValidateEntry first, before the other events?

The reason is that when another row is selected, I remove goEditing with causes OnValidateEntry to not trigger:

Code: Pascal  [Select][+][-]
  1. procedure sgdGridsSelectCell(Dummy: Pointer; Sender: TObject; aCol, aRow: Integer; var CanSelect: Boolean);
  2. begin
  3.   if CurrentGridRow <> aRow then
  4.     StringGrid1.Options := StringGrid1.Options - [goEditing,goRelaxedRowSelect,goAlwaysShowEditor] + [goRowSelect,goRowHighlight]
  5.     // the previous cell is now not validated
  6.  

Jonny

  • Full Member
  • ***
  • Posts: 144
Re: Unselect all rows in stringgrid
« Reply #22 on: February 26, 2025, 06:59:31 pm »
The ValidateEntry function cannot be triggered by my application because it is in the protected area of TCustomGrid=class(TCustomControl) therefore inaccessible.

Looking through lazarus/lcl/grids.pas shows no indication of order of event to my untrained eyes.

Is there a way to override the class somehow?

 

TinyPortal © 2005-2018