Forum > LCL

[SOLVED] TStringGrid.OnDblClick-Event: how distinguish title row from normal row

(1/2) > >>

Hartmut:
I have a TStringGrid with an OnDblClick-Event and in this event I want to get the row# which was double-clicked and work with them. But if the title row was double-clicked, nothing should happen.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---StringGrid1.Rowdoes only work, as long as I double-click in a normal cell, but when I make a double-click in the title row, I get the last row# where the cursor has been before. So I cannot distinguish, if the title row was double-clicked.

How can I distinguish in this event, whether a normal cell or a title cell was double-clicked?

Thanks in advance. I use Lazarus 1.6.2 with FPC 3.0.0 on Win7.

howardpc:
You could adapt this sort of idea:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  TForm1 = class(TForm)    StringGrid1: TStringGrid;    procedure StringGrid1DblClick(Sender: TObject);    procedure StringGrid1HeaderClick(Sender: TObject; IsColumn: Boolean;      Index: Integer);  private    FHeaderClicked: boolean;  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.StringGrid1DblClick(Sender: TObject);begin  if FHeaderClicked then begin    WriteLn('You double-clicked on a fixed row cell');    FHeaderClicked:=False;  end  else WriteLn('You double-clicked on a non-fixed row cell');end; procedure TForm1.StringGrid1HeaderClick(Sender: TObject; IsColumn: Boolean;  Index: Integer);begin  FHeaderClicked:=True;end;

wp:
Or this, inspired by scrolling through the public TCustomGrid methods in grids.pas:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.StringGrid1DblClick(Sender: TObject);var  p: TPoint;  gz: TGridZone;begin  p := StringGrid1.ScreenToClient(Mouse.CursorPos);  gz := StringGrid1.MouseToGridZone(p.x, p.y);  case gz of    gzNormal: ShowMessage('Normal');    gzFixedCols: ShowMessage('FixedCols');    gzFixedRows: ShowMessage('FixedRows');    gzFixedCells: ShowMessage('FixedCells');    gzInvalid: ShowMessage('Invalid');  end;end;  

Hartmut:
Thanks a lot to howardpc and wp for your suggestions.

Howardpc's idea looks a bit tricky to me, because StringGrid1HeaderClick() must ALWAYS be called before StringGrid1DblClick(). In some experiments this was the case, but is this always sure? (why?) I'm a little afraid...

I have tried wp's idea too and this seems to be the perfect solution. Problem #1 solved :-)

But now I see a 2nd problem: I have a need for event StringGrid1HeaderClick(), which I wanted to develop next. But this event is called, even if I make a double-click! How do I prevent StringGrid1HeaderClick() to react on a double-click? Or how to distinguish between double- and single-click?

Thanks in advance for your help!

wp:

--- Quote from: Hartmut on July 15, 2017, 11:03:33 pm ---I have a need for event StringGrid1HeaderClick(), which I wanted to develop next.
--- End quote ---
Why develop? It's already there...


--- Quote from: Hartmut on July 15, 2017, 11:03:33 pm ---But this event is called, even if I make a double-click! How do I prevent StringGrid1HeaderClick() to react on a double-click?
--- End quote ---
In the same way as shown above?


--- Quote from: Hartmut on July 15, 2017, 11:03:33 pm ---Or how to distinguish between double- and single-click?
--- End quote ---
I suppose you need a timer, its interval is a bit longer than the double-click interval. In MouseDown and MouseUp you take actions to avoid firing the OnClick event, but trigger the timer. The OnClick, instead, is fired in timer's OnTimer event. If you disable the timer in the DblClick method the OnClick never happens in case of a DblClick.

Navigation

[0] Message Index

[#] Next page

Go to full version