TStringGrid.row and onclick event lazarus / 31-LCL ---------------------------------------------------------------------- straetch · 2018-03-22 09:09 When I execute stringgrid1.row := 1 ,  then the onclick event of the stringgrid is automatically fired. I want to distinguish between setting the row index somewhere in my program (to highlight the row) and an actual rowclick by a user. How can I test the difference in the click handler? I tried   if (Sender is TStringGrid)    in the handler, but this returns true in both cases. ---------------------------------------------------------------------- howardpc · 2018-03-22 10:03 AFAIK there is no way to distinguish the source of such clicks from the grid's OnClick handler alone since the Sender parameter does not provide that information. You have to get the data indirectly. You could use code such as what is shown below. In a new Lazarus project drop a grid, label and button on the main form and name them as shown. Generate an OnClick handler for the button and complete the code as follows: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, Forms, Controls, Grids, StdCtrls; type   TForm1 = class(TForm)    SetRowTo1Button: TButton;    grid: TStringGrid;    WhoClickedTheGridLabel: TLabel;    procedure gridMouseUp(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure SetRowTo1ButtonClick(Sender: TObject);    procedure gridClick(Sender: TObject);  private    FRowClickedInCode: Boolean;    procedure UpdateClickReport;  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.gridClick(Sender: TObject);begin  UpdateClickReport;  if FRowClickedInCode then    FRowClickedInCode := False;end; procedure TForm1.UpdateClickReport;var  i: Integer;begin  WhoClickedTheGridLabel.Caption := '...'; // to force a slight delay  for i:= 0 to 100000000 do;               // so you can distinguish  Application.ProcessMessages;             // separate user clicks   case FRowClickedInCode of    True:  WhoClickedTheGridLabel.Caption := 'click generated via code';    False: WhoClickedTheGridLabel.Caption := 'click generated by user grid click';  end;end; procedure TForm1.SetRowTo1ButtonClick(Sender: TObject);var  gridRowChanged: Boolean;begin  gridRowChanged := grid.Row <> 1;  FRowClickedInCode := True;  grid.Row := 1;  if not gridRowChanged then    UpdateClickReport;end; procedure TForm1.gridMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin  UpdateClickReport;end; end. ---------------------------------------------------------------------- ASerge · 2018-03-22 10:12 Quote from: straetch on March 22, 2018, 10:09:54 amWhen I execute stringgrid1.row := 1 ,  then the onclick event of the stringgrid is automatically fired. I want to distinguish between setting the row index somewhere in my program (to highlight the row) and an actual rowclick by a user. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i type  TGridWithGridState = class helper for TCustomGrid  strict private    function GetGridState: TGridState; inline;  public    property GridState: TGridState read GetGridState;  end; function TGridWithGridState.GetGridState: TGridState;begin  Result := FGridState;end; procedure TForm1.Button1Click(Sender: TObject);begin  StringGrid1.Row := 2;end; procedure TForm1.StringGrid1Click(Sender: TObject);begin  if StringGrid1.GridState = gsSelecting then    Memo1.Append('Click by mouse')  else    Memo1.Append('Change by program')end; ---------------------------------------------------------------------- straetch · 2018-03-23 08:47 Great, Two different solutions that work: a workaround at the program code level and one that gives access to protected members of the grid. I appreciate very much that I can rely on prompt expert help where my own skills are limited. Many thanks. ---------------------------------------------------------------------- mangakissa · 2020-04-16 12:21 Thank you howard en aSerge. The second solution works for me. Itis still going on. By changing TStringgrid.row then even TStringgrid.Onclick is fired. I'm working with Lazarus 2.08 / FPC 3.04. I don't  a bug report is created, but if it is I will create one.