* * *

Author Topic: [SOLVED] TStringGrid.OnDblClick-Event: how distinguish title row from normal row  (Read 806 times)

Hartmut

  • Full Member
  • ***
  • Posts: 115
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  [Select]
  1. StringGrid1.Row
does 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.
« Last Edit: July 16, 2017, 10:43:55 am by Hartmut »

howardpc

  • Hero Member
  • *****
  • Posts: 2422
You could adapt this sort of idea:
Code: Pascal  [Select]
  1.   TForm1 = class(TForm)
  2.     StringGrid1: TStringGrid;
  3.     procedure StringGrid1DblClick(Sender: TObject);
  4.     procedure StringGrid1HeaderClick(Sender: TObject; IsColumn: Boolean;
  5.       Index: Integer);
  6.   private
  7.     FHeaderClicked: boolean;
  8.   end;
  9.  
  10. var
  11.   Form1: TForm1;
  12.  
  13. implementation
  14.  
  15. {$R *.lfm}
  16.  
  17. { TForm1 }
  18.  
  19. procedure TForm1.StringGrid1DblClick(Sender: TObject);
  20. begin
  21.   if FHeaderClicked then begin
  22.     WriteLn('You double-clicked on a fixed row cell');
  23.     FHeaderClicked:=False;
  24.   end
  25.   else WriteLn('You double-clicked on a non-fixed row cell');
  26. end;
  27.  
  28. procedure TForm1.StringGrid1HeaderClick(Sender: TObject; IsColumn: Boolean;
  29.   Index: Integer);
  30. begin
  31.   FHeaderClicked:=True;
  32. end;

wp

  • Hero Member
  • *****
  • Posts: 3956
Or this, inspired by scrolling through the public TCustomGrid methods in grids.pas:

Code: Pascal  [Select]
  1. procedure TForm1.StringGrid1DblClick(Sender: TObject);
  2. var
  3.   p: TPoint;
  4.   gz: TGridZone;
  5. begin
  6.   p := StringGrid1.ScreenToClient(Mouse.CursorPos);
  7.   gz := StringGrid1.MouseToGridZone(p.x, p.y);
  8.   case gz of
  9.     gzNormal: ShowMessage('Normal');
  10.     gzFixedCols: ShowMessage('FixedCols');
  11.     gzFixedRows: ShowMessage('FixedRows');
  12.     gzFixedCells: ShowMessage('FixedCells');
  13.     gzInvalid: ShowMessage('Invalid');
  14.   end;
  15. end;  
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Hartmut

  • Full Member
  • ***
  • Posts: 115
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

  • Hero Member
  • *****
  • Posts: 3956
I have a need for event StringGrid1HeaderClick(), which I wanted to develop next.
Why develop? It's already there...

But this event is called, even if I make a double-click! How do I prevent StringGrid1HeaderClick() to react on a double-click?
In the same way as shown above?

Or how to distinguish between double- and single-click?
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.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Hartmut

  • Full Member
  • ***
  • Posts: 115
I did not want to develop the StringGrid1HeaderClick() event itself, I meant only to develop its "content".
I hoped, that there would be an easier way to distinguish between double- and single-click, but I learned that a timer is neccessary.
Thanks again a lot to wp for his help.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus