Recent

Author Topic: String grid dbl click question  (Read 8022 times)

jbmckim

  • Full Member
  • ***
  • Posts: 144
String grid dbl click question
« on: June 14, 2018, 12:14:51 am »
I'm puzzled by the SG dbl click event behavior.

I'm trying to dbl click on a column header, check to see if I'm in a given column and then do some work based on being in the given column.  This almost works except that when the column header is the first thing "clicked," the MyStringGrid.SelectedColumn.Index is given as zero.  Clicking anywhere else on the form first and MyStringGrid.SelectedColumn.Index is = to the appropriate column.  I'd like to have the column index be relevant from the first click.

Thanks.

P.S. Puzzling but irrelevant, I tried to dummy up a SG to post here but it behaves differently.  (Possibly based on SG Options but that's not really my problem here.)


wp

  • Hero Member
  • *****
  • Posts: 11830
Re: String grid dbl click question
« Reply #1 on: June 14, 2018, 12:37:42 am »
This is working for me: Use the event OnHeaderClick which gives you the clicked column index as a parameter. Store the index and use it in the OnDblClick.

Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.     StringGrid1: TStringGrid;
  4.     procedure StringGrid1DblClick(Sender: TObject);
  5.     procedure StringGrid1HeaderClick(Sender: TObject; IsColumn: Boolean;
  6.       Index: Integer);
  7.   private
  8.     FColClicked: Integer;
  9.   end;
  10. //...
  11. procedure TForm1.StringGrid1HeaderClick(Sender: TObject; IsColumn: Boolean;
  12.   Index: Integer);
  13. begin
  14.   if IsColumn then FColClicked := Index;
  15. end;
  16.  
  17. procedure TForm1.StringGrid1DblClick(Sender: TObject);
  18. begin
  19.   ShowMessage('Double click in column ' + IntToStr(FColClicked));
  20. end;  

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: String grid dbl click question
« Reply #2 on: June 15, 2018, 02:00:21 am »
This is almost right, however a problem still remains.

If I click anywhere on the grid and THEN click the header,  OnHeaderClick is executed.  If however, my first click is on the column header, OnHeaderClick is not executed.  (I verified focus by posting a break in OnMouseDown.)

OnMouseDown does not have the row/column coordinates available the first time through (i.e. as above) so I can't use that.

The grid is on a form with a TAChart and another stringgrid.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: String grid dbl click question
« Reply #3 on: June 15, 2018, 09:13:19 am »
I cannot confirm that OnHeaderClick does not fire always.

But anyway, maybe this is what you need: the grid has this method:
Code: Pascal  [Select][+][-]
  1.     procedure MouseToCell(X,Y: Integer; var ACol,ARow: Longint);

fred

  • Full Member
  • ***
  • Posts: 201
Re: String grid dbl click question
« Reply #4 on: June 15, 2018, 09:30:26 am »
I just converted an old Delphi 5 program to Lazarus and used in DblClick:

Code: Pascal  [Select][+][-]
  1.   P := grid.ScreenToClient(Mouse.CursorPos);
  2.   grid.MouseToCell(P.X, P.Y, myCol, myRow);

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: String grid dbl click question
« Reply #5 on: June 15, 2018, 07:42:30 pm »
Those things didn't work but it did get me to thinking.

OnMouseDown does supply a X/Y.  Is there a cheap (i.e. efficient from a coding standpoint) of mapping the X/Y coord returned to the boundaries of the column header?

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: String grid dbl click question
« Reply #6 on: June 15, 2018, 08:52:36 pm »
I guess you don't read what fred and I were writing...

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: String grid dbl click question
« Reply #7 on: June 15, 2018, 10:53:19 pm »
Sure I did.  (See  - "Those things didn't work" from my previous response.)  You left a question or two in your response though, perhaps not providing a context you assumed to be present. To be clear in this response, executing grid.MouseToCell(P.X, P.Y, myCol, myRow); from OnMouseDown changes/fixes nothing.  The problem remains as it was initially. 

I may be wrong as to where/how you intended the snippet to be used but my take seems to be consistent with my previous questions.  Again, on my grid OnDblClick only executes when the double click occurs on the column title.  That would be fine, except that incurs the problem I'm asking about.  (i.e. Only executes after a cell on the grid has first been clicked.)

Where do you intend that MouseToCell should be called from?  How are you proposing it be used? 


It occurs to me that there is an off chance this might be an issue with my option set vs. yours.  Here's mine:

Code: Pascal  [Select][+][-]
  1. [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goRangeSelect,goEditing,goAlwaysShowEditor,goSmoothScroll]

thanks.



wp

  • Hero Member
  • *****
  • Posts: 11830
Re: String grid dbl click question
« Reply #8 on: June 15, 2018, 11:39:29 pm »
In the attachment you find a working demo which show fred's and my ideas. You can select them with the radiobuttons at the bottom of the form.

[EDIT]
Attachment added.
« Last Edit: June 15, 2018, 11:59:52 pm by wp »

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: String grid dbl click question
« Reply #9 on: June 15, 2018, 11:57:33 pm »
Sorry,

I don't see an attachment.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: String grid dbl click question
« Reply #10 on: June 16, 2018, 12:04:47 am »
added, sorry

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: String grid dbl click question
« Reply #11 on: June 19, 2018, 04:39:00 pm »
Thanks for the update.

The relevance of the example puzzles me a bit.  I meant to be asking about issues trapping an event from a column header click (or double click) event.  The project posted doesn't show any grid events at all so I don't understand how it addresses the issue.

Thanks again.

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: String grid dbl click question
« Reply #12 on: June 19, 2018, 07:02:59 pm »
Then I cannot help you. To me the project clearly shows how a double-click on a header can be detected and evaluated to determine the column index.

jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: String grid dbl click question
« Reply #13 on: June 19, 2018, 10:15:50 pm »
We must not be communicating.  My question regards the behavior of the SomeGrid.OnClick (or OnDblClick) event(s).  In your example, the grid does not implement those events so I don't understand how they're supposed to help.  All actions to the grid in the example drive off of push buttons.

Anyone else care to give the question a go?


jbmckim

  • Full Member
  • ***
  • Posts: 144
Re: String grid dbl click question
« Reply #14 on: June 19, 2018, 10:44:29 pm »
Put another way,  here is code from my grid edited for purposes of what we're doing here:

Code: Pascal  [Select][+][-]
  1. procedure TExitMainForm.InputStringGridDblClick(Sender: TObject);
  2. var
  3.   Reply, BoxStyle, ColNum, RowNum, i : integer;
  4.   strInput : string = '';
  5.   bReply : boolean = false;
  6.   iUserInt : integer = 0;
  7.  
  8.  
  9. begin
  10.   ColNum := InputStringGrid.SelectedColumn.Index;
  11.   RowNum := InputStringGrid.Row;
  12.  
  13. end;          

Note " TExitMainForm.InputStringGridDblClick".  This is the grid's double click event.  (It also has a single click event.)  When I search the code for the project you uploaded, I do not find the strings "DblClick" or "GridClick."  Therefore, I do not understand how the example is supposed to answer my question.  This does not seem like an unreasonable question to me.

 

TinyPortal © 2005-2018