Recent

Author Topic: TStringGrids problem(Solved)  (Read 575 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1259
TStringGrids problem(Solved)
« on: November 21, 2022, 06:05:36 am »
Setting up a demo on grids and this answered most of my questions on grids except how to
set the option goRowSelect, and prepare Canvas not working.

Demo attached.
Thanks.
 
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnGoClick(Sender: TObject);
  2. begin
  3.   GridSetup(GRID1, 2,'Uses','Procedure''s');
  4.   With Grid1 do begin
  5. //   Options2:=[goRowSelect];         <== Error of no member      
  6.    end;
  7.   LoadGrid;
  8. end;
  9.  
  10. procedure TForm1.Grid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  11.   aState: TGridDrawState);
  12. begin
  13.   if (Sender as TStringGrid).ColCount > 1 then
  14.   begin
  15.     if (Sender as TStringGrid).Cells[1, aRow] = '1' then
  16.       (Sender as TStringGrid).Canvas.Font.Color :=  clRed
  17.     else
  18.       (Sender as TStringGrid).Canvas.Font.Color :=  clBlack;
  19.   end;
  20. end;      
  21.  
  22.  
  23.  
  24.  
« Last Edit: November 21, 2022, 11:01:37 pm by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

dje

  • Full Member
  • ***
  • Posts: 132
Re: TStringGrids problem
« Reply #1 on: November 21, 2022, 06:26:49 am »
Unsure why you have the code Cells[1, aRow] = '1', when there are no '1' strings in your data file.
If I change that to Cells[1, aRow] = 'classes', I indeed have the 'classes' rows drawn as red.
Im also unsure why you have Grid1.Enabled := False;  in the method Grid1Click.
But, yes, OnPrepareCanvas has worked fine for me.

Note:: To set goRowSelect use:
Code: [Select]
Options:=Options+[goRowSelect];
« Last Edit: November 21, 2022, 06:30:05 am by dje »

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: TStringGrids problem
« Reply #2 on: November 21, 2022, 08:31:33 am »
well I don't know grids that well, but I'll look at the  'Cells[1, aRow] = '1' and try to figure what you mean. The enable:=False is to stop the selection and format a call to a procedure.                                                                                                                                                                                                                                                from the info i have. I may return to the Grid so I'll enable on return.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

paweld

  • Hero Member
  • *****
  • Posts: 543
Re: TStringGrids problem
« Reply #3 on: November 21, 2022, 01:57:50 pm »
I modified your example a bit. I used the code from another topic ( https://forum.lazarus.freepascal.org/index.php/topic,61287.msg460679.html#msg460679 ) to get selected cell.
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 10454
Re: TStringGrids problem
« Reply #4 on: November 21, 2022, 03:13:52 pm »
I modified your example a bit. I used the code from another topic ( https://forum.lazarus.freepascal.org/index.php/topic,61287.msg460679.html#msg460679 ) to get selected cell.
Again: Why so complicated? The selected cell in a StringGrid is given by column index StringGrid1.Col and row index Stringgrid1.Row:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Grid1Click(Sender: TObject);
  2. begin
  3.   Label1.Caption := Format('Selected %d ROW and %d COL', [Grid1.Row, Grid1.Col]);
  4.   Label2.Caption := 'Selected value: ' + Grid1.Cells[Grid1.Col, Grid1.Row];
  5. end;
  6.  

paweld

  • Hero Member
  • *****
  • Posts: 543
Re: TStringGrids problem
« Reply #5 on: November 21, 2022, 03:29:52 pm »
@wp: I don't know what the OP wants to achieve, but you can click outside the column, and the Col property returns the number of the last column.
But this only applies to this example, because in the linked post I unnecessarily complicated the code.
« Last Edit: November 21, 2022, 03:33:53 pm by paweld »
Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 10454
Re: TStringGrids problem
« Reply #6 on: November 21, 2022, 04:24:24 pm »
@wp: I don't know what the OP wants to achieve, but you can click outside the column, and the Col property returns the number of the last column.
There is also a property AllowOutboundEvents (public only --> not in Object Inspector!) which, if set to false, fires the OnClick only when the click is within the grid area. When the click occurs beyond the last column there is no event at all: https://wiki.lazarus.freepascal.org/Grids_Reference_Page#property_AllowOutboundEvents

paweld

  • Hero Member
  • *****
  • Posts: 543
Re: TStringGrids problem
« Reply #7 on: November 21, 2022, 06:57:49 pm »
I didn't know about this property thank you for the valuable tip.
« Last Edit: November 21, 2022, 07:18:13 pm by paweld »
Best regards / Pozdrawiam
paweld

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: TStringGrids problem
« Reply #8 on: November 21, 2022, 11:00:40 pm »
@paweld Exactly what I was trying to achieve. PERFECT!

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018