Recent

Author Topic: TStringgrid question  (Read 890 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
TStringgrid question
« on: November 20, 2022, 02:25:17 am »
I have a TStringgrid On a Form loaded with 2 Col and 60 or so rows. I would like to scroll the rows and click on one of the rows. Highlight the row and retrieve the data in column 1, row selected.

 I have programed the OnClick and OnSelectedCell but can't make this work.

Need help

Thanks
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

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2048
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TStringgrid question
« Reply #1 on: November 20, 2022, 04:31:28 am »
Use what the manual say, OnSelection.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1Selection(Sender: TObject; aCol, aRow: Integer);
  2. begin
  3.   Memo1.Lines.Add(StringGrid1.Cells[aCol, aRow]);
  4. end;
Hope you figure out how to use it for your task.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringgrid question
« Reply #2 on: November 20, 2022, 05:50:05 am »
Not what I'm looking for. If I scroll the Grid OnSelection fires. I only want it to fire if I click on a row. Otherwise I should be able to continue scrolling until I find what I want.

I guess I could let it fire on scrolling  but when OnClick fires take the contents at that point. It would work if I used a global variable maybe
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: 989
Re: TStringgrid question
« Reply #3 on: November 20, 2022, 08:01:22 am »
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLIntf, LCLType;
  3.  
  4. procedure TForm1.sgClick(Sender: TObject);
  5. var                      
  6.   x, y, tbh: Integer;
  7.   aCol, aRow: Integer;
  8. begin
  9.   tbh := GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CYSMCAPTION);  //title bar height
  10.   x := Mouse.CursorPos.X - Form1.BoundsRect.Left - sg.Left;
  11.   y := Mouse.CursorPos.Y - tbh - Form1.BoundsRect.Top - sg.Top;
  12.   sg.MouseToCell(x, y, aCol, aRow);
  13.   caption := sg.Cells[aCol, aRow];
  14. end;              
Best regards / Pozdrawiam
paweld

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2048
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TStringgrid question
« Reply #4 on: November 20, 2022, 12:09:51 pm »
I only want it to fire if I click on a row.
I do not understand what you mean, my above gets fired when you click a cell not when you scroll, does "click on a row" mean you want that it only fire when you click on a header?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: TStringgrid question
« Reply #5 on: November 20, 2022, 12:16:15 pm »
The Grid already has a ROW and COL property that tells you which one has just been selected via click.

 Use the OnClick event and query the COL / ROL property of the grid.

 Btw.
   there is a TValueEditor that handles 2 cols for tag name and values, just in case that is what you are trying. It too is made from a string grid.

The only true wisdom is knowing you know nothing

Zvoni

  • Hero Member
  • *****
  • Posts: 2327
Re: TStringgrid question
« Reply #6 on: November 20, 2022, 06:03:45 pm »
I only want it to fire if I click on a row.
I do not understand what you mean, my above gets fired when you click a cell not when you scroll, does "click on a row" mean you want that it only fire when you click on a header?
I think he means with scrolling: there is a row highlighted, and he „scrolls“ through the grid using up/down arrow key, meaning the highlighted row moves, but is not selected
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TStringgrid question
« Reply #7 on: November 20, 2022, 06:57:29 pm »
I'll make a demo to explain what I mean.
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

itblumi

  • New Member
  • *
  • Posts: 29
Re: TStringgrid question
« Reply #8 on: November 20, 2022, 06:59:30 pm »
You can do this only when you override the Grid and not inherited from the virtual Click; procedure of the TStringGrid.
In this procedure is implented the key up/down select.

Code: Pascal  [Select][+][-]
  1. // highlight a row
  2. var
  3.   ARect: TGridRect;
  4. begin
  5.   ARect.Left := 0; // column  start
  6.   ARect.Top:=2; // row 3 you have to
  7.   ARect.Height := 0; // 1 row highlighted
  8.   ARect.Width := StringGrid1.ColCount;
  9.   StringGrid1.Selection := ARect;
  10. end;
  11.  
« Last Edit: November 20, 2022, 07:58:21 pm by itblumi »
Jan

Delphi XE6, Lazarus 2.2.4, Visual Studio, Eclipse
Platforms: Ubuntu 22.10, Windows 7, 10
Progarmming languages: Pascal, C, C++, C#, Java

itblumi

  • New Member
  • *
  • Posts: 29
Re: TStringgrid question
« Reply #9 on: November 20, 2022, 07:47:14 pm »
Here the OnClick event of the grid for this

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1Click(Sender: TObject);
  2. var
  3.   ARect: TGridRect;
  4. begin
  5.   ARect.Left := 0; // column  start
  6.   ARect.Top := StringGrid1.Row; // new selected row
  7.   ARect.Height := 0;
  8.   ARect.Width := StringGrid1.ColCount;
  9.   StringGrid1.Selection := ARect;
  10.  
  11.   // get the data of col 1 and the selected row
  12.   ShowMessage(StringGrid1.Cells[1, StringGrid1.Row]);
  13. end;
  14.  
  15.  

In this example is the clicked cell not highlighted, you can do this only with the options of the StringGrid goRowSelect.
« Last Edit: November 20, 2022, 07:58:08 pm by itblumi »
Jan

Delphi XE6, Lazarus 2.2.4, Visual Studio, Eclipse
Platforms: Ubuntu 22.10, Windows 7, 10
Progarmming languages: Pascal, C, C++, C#, Java

wp

  • Hero Member
  • *****
  • Posts: 11906
Re: TStringgrid question
« Reply #10 on: November 20, 2022, 07:58:12 pm »
I would like to scroll the rows and click on one of the rows. Highlight the row and retrieve the data in column 1, row selected.
I don't understand how the discussion here is related to the original question. Unless I don't completely misunderstand it the request can be achieved by
- adding goRowSelect to the options of the grid (in order to highlight the active row)
- using the following OnClick handler which retrieves the cell text in column 1 of the selected row, here displaying it in the form's Caption for simplicity:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1Click(Sender: TObject);
  2. begin
  3.   Caption := StringGrid1.Cells[1, StringGrid1.Row];
  4. end;

 

TinyPortal © 2005-2018