Recent

Author Topic: TStringGrid Row & Col problems  (Read 435 times)

simone

  • Sr. Member
  • ****
  • Posts: 260
TStringGrid Row & Col problems
« on: August 02, 2019, 11:22:56 pm »
I need to process data stored in cells of a stringgrid after they are edited/modified by user. At this purpose I use Row and Col property inside OnEditingDone event handler to detect what cell is changed, but it seems to me that above properties return random values (see attached program demo, built with Lazarus 2.0.2 - Win64)… Is it a bug? Thanks for the help

wp

  • Hero Member
  • *****
  • Posts: 6471
Re: TStringGrid Row & Col problems
« Reply #1 on: August 02, 2019, 11:42:39 pm »
Looks pretty logical to me. What exactly is displayed for which cell?

I modified your OnEditingDone such that the Col:Row indexes are written back to the edited cell, and I get what I am expecting:
Code: Pascal  [Select]
  1. procedure TForm1.StringGrid1EditingDone(Sender: TObject);
  2. begin
  3.   StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] :=
  4.     'C'+StringGrid1.Col.ToString+':R'+StringGrid1.Row.ToString;
  5. //  Edit1.Text:='C'+StringGrid1.Col.ToString+':R'+StringGrid1.Row.ToString;
  6. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

simone

  • Sr. Member
  • ****
  • Posts: 260
Re: TStringGrid Row & Col problems
« Reply #2 on: August 03, 2019, 12:20:20 am »
Thanks wp. You are right: the behavior is correct. I misunderstood the values of row and col.

Another question: Using 'OnEditingDone' I can only detect changes of cell content after editing is terminated. It's possible to handle changes while typing? Thank again. 

wp

  • Hero Member
  • *****
  • Posts: 6471
Re: TStringGrid Row & Col problems
« Reply #3 on: August 03, 2019, 12:43:43 pm »
You can hook into the OnSetEditText event which passes the contents of the cell editor to the grid after each key press:

Code: Pascal  [Select]
  1. procedure TForm1.StringGrid1SetEditText(Sender: TObject; ACol, ARow: Integer;
  2.   const Value: string);
  3. begin
  4.   Edit1.Text := Value;
  5. end;
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

simone

  • Sr. Member
  • ****
  • Posts: 260
Re: TStringGrid Row & Col problems
« Reply #4 on: August 03, 2019, 01:10:14 pm »
Thanks Wp. I was looking for a property named 'OnChange', as in others components. I didn't realize that 'OnSetEditText' could be useful for this purpose.

wp

  • Hero Member
  • *****
  • Posts: 6471
Re: TStringGrid Row & Col problems
« Reply #5 on: August 03, 2019, 01:28:46 pm »
It is probably named like this because it belongs to the setter/getter events for interaction with the cell editor: OnGetCellText passes the current cell text to the cell editor when editing become active, and OnSetCellText returns the currently edited text back to the grid. The choice of "Set"/"Get" is a bit strange here, though.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10