Lazarus

Using the Lazarus IDE => General => Topic started by: ineedhelplul on May 05, 2018, 07:17:06 pm

Title: I need help with StringGrids
Post by: ineedhelplul on May 05, 2018, 07:17:06 pm
Hey,

i dont know if im in the right Forum but i need help.
I want to program a battleships gamemode for a school project. I just cant find how i can make it so if you click on a cell a action happens. So i want to know how i can make a procedure for clicking a cell.

Thanks
Title: Re: I need help with StringGrids
Post by: howardpc on May 05, 2018, 07:57:14 pm
Here's a simple example to get you started. Note that the OnClick event of TStringGrid is not fired for Fixed rows/columns.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Grids;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     StringGrid1: TStringGrid;
  16.     procedure StringGrid1Click(Sender: TObject);
  17.   private
  18.     procedure DoActionAtCell(aCol, aRow: Integer);
  19.   public
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. procedure TForm1.DoActionAtCell(aCol, aRow: Integer);
  30. begin
  31.   ShowMessageFmt('Clicked col:%d, row:%d',[aCol, aRow]);
  32. end;
  33.  
  34. procedure TForm1.StringGrid1Click(Sender: TObject);
  35. var
  36.   grd: TStringGrid absolute Sender;
  37. begin
  38.   if not (Sender is TStringGrid) then
  39.     Exit;
  40.   DoActionAtCell(grd.Col, grd.Row);
  41. end;
  42.  
  43. end.
TinyPortal © 2005-2018