Recent

Author Topic: TStringGrid select cell or row programmatically  (Read 1112 times)

Чебурашка

  • Hero Member
  • *****
  • Posts: 588
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
TStringGrid select cell or row programmatically
« on: June 23, 2023, 10:04:36 am »
Hello,
prob I did not see so well the documentation, but is possible what written in topic?

The typical situation in which I would need to do this, is when I have a populated string grid, for example with a cell or a row previously selected, for example by means of mouse operation (one could imagine also a situation where there are multiple items selected, but in my case this is not important).

For some external reason, the string grid contents are modified.

I would like to restore the selection as much as possible as it was prior to the string contents modification (for example if the selected cell does no longer exist, ok, I won't try re-selecting it).

The startegy is the following:

1. when selecion is initially done, I store in a dedicated strcture (apart from the string grid) the information about what has been selected, I call this structure "selectioninformation"
2. after grid content gets modified, I scan the selectioninformation and for each item in there, see if there is still in the new string grid contents such a item, if so "set it selected".

The question here is the "set it selected" which I do not find how to do.

Other frameworks I used in previous programmer life, have something like

Code: Pascal  [Select][+][-]
  1.     Cell[x, y].Selected := True
  2.     // or
  3.     Row[x].Selected := True;
  4.  

What is the "official" way to do that with StringGrids?

Thanks
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

wp

  • Hero Member
  • *****
  • Posts: 12909
Re: TStringGrid select cell or row programmatically
« Reply #1 on: June 23, 2023, 11:06:53 am »
You say: "one could imagine also a situation where there are multiple items selected, but in my case this is not important". Do I understand correctly that you understand selection as a single rectangular block of cells? In this case, the attached project probably is what you need.

Чебурашка

  • Hero Member
  • *****
  • Posts: 588
  • СЛАВА УКРАЇНІ! / Slava Ukraïni!
Re: TStringGrid select cell or row programmatically
« Reply #2 on: June 23, 2023, 11:26:57 am »
You say: "one could imagine also a situation where there are multiple items selected, but in my case this is not important". Do I understand correctly that you understand selection as a single rectangular block of cells? In this case, the attached project probably is what you need.

Super, thanks.

In reality my need was just be able to select a certain row (all the row)

I found that if the stringgrid is configured with goRowSelect, I just need to do (assuming grid han 10 rows and I want select 4-th):

Code: Pascal  [Select][+][-]
  1.     StringGrid.Row := 4;
  2.  

Sorry I did not find it in the docs.
FPC 3.2.0/Lazarus 2.0.10+dfsg-4+b2 on Debian 11.5
FPC 3.2.2/Lazarus 2.2.0 on Windows 10 Pro 21H2

 

TinyPortal © 2005-2018