tStringGrid Mouse click on area below last row. lazarus / 31-LCL ---------------------------------------------------------------------- iru · 2022-03-22 06:06 Gentlefolk, I saw an answer somewhere about this a long time ago but cant find it.... I have a StringGrid with a couple of rows displayed and a large empty area below the last row. If the user accidentaly  clicks on the empty area it is the same as clicking the the last row. Not nice! Any ideas, code on how to change/fix this? Ian. ---------------------------------------------------------------------- Zvoni · 2022-03-22 07:25 The OnMouseDown ships the x and y of your Mouse-Click inside the StringGrid. Just compare them to the ClientRect of the Grid ---------------------------------------------------------------------- wp · 2022-03-22 09:15 Set the grid's AllowOutboundEvents to false: https://wiki.lazarus.free pascal.org/Grids_Reference_Page#property_AllowOutboundEvents ---------------------------------------------------------------------- alpine · 2022-03-22 10:38 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 TCustomGrid.MouseToCell(X, Y: Integer; var ACol, ARow: Longint);Will return ACol=-1 or ARow=-1 when not in a cell. ---------------------------------------------------------------------- Zvoni · 2022-03-22 10:38 Quote from: wp on March 22, 2022, 10:15:27 amSet the grid's AllowOutboundEvents to false: https://wiki.lazarus.freepascal.org/Grid s_Reference_Page#property_AllowOutboundEvents Yepp, that's the one. Just tested it https://lazarus- ccr.sourceforge.io/docs/lcl/grids/tcustomgrid.allowoutboundevents.html ---------------------------------------------------------------------- iru · 2022-03-23 21:40 Gentlefolk, Thank you for your responses, I will cut some code.... ian. ---------------------------------------------------------------------- iru · 2022-03-30 09:41 Environment: Linux Mint 20, FPC 3.2.2, LAZ 2.0.12. Gentlefolk, I set the AllowOutboundEvents := False on the StringGrid. When I click on an grid area outside the defined rows/colums I get a programmed exception. Unit Grid line# 3137 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  procedure TCustomGrid.SetRow(AValue: Integer);begin  if AValue=FRow then Exit;  if not AllowOutBoundEvents then <------------------Note    CheckLimitsWithError(FCol, AValue);  MoveExtend(False, FCol, AValue, True);  Click;end;  // The following code is then entered with aRow = -1 // Unit Grid line# 8038 procedure TCustomGrid.CheckLimitsWithError(const aCol, aRow: Integer);begin  if (aCol < 0) or (aRow < 0) or (aCol >= ColCount) or (aRow >= RowCount) then    raise EGridException.Create(rsGridIndexOutOfRange);end; So where to from here??? Ian. ---------------------------------------------------------------------- wp · 2022-03-30 10:00 This must be generated somewhere in your own code (of course, you cannot select a cell outside the grid). In the bare StringGrid, AllowOutboundEvents works correctly - see attached demo. ---------------------------------------------------------------------- iru · 2022-03-31 06:15 WP. Thank you for the response. You are wrong and you are right..... In your example you cannot select a cell outside the grid because the cells occupy the entire space of the grid. My code ties the top, left, width and bottom of the grid to a large panel (for display requirements). With only one or two rows of cells in the grid there is a large empty space under the bottom row of cells. Click on that (outside the cells but inside the grid space) and with the AllowOutboundEvents = False the exception is raised! I can resolve this by filling the empty display space with empty rows so that no 'outside of cells' space is available for the user to click on or I can write some code to work out that the click is outside the displayed rows and ignore the click. I code on.... Thanks, Ian. ---------------------------------------------------------------------- wp · 2022-03-31 08:13 Quote from: iru on March 31, 2022, 08:15:10 amIn your example you cannot select a cell outside the grid because the cells occupy the entire space of the grid. How can you say that? The cells do occupy only part of the grid in the demo - look at the attached screenshot. Or do you mean something different? Quote from: iru on March 31, 2022, 08:15:10 amMy code ties the top, left, width and bottom of the grid to a large panel (for display requirements). What does this mean in terms of code? Please post a sample project showing the issue. otherwise there's just wild guessing on our side what you might be doing. ---------------------------------------------------------------------- iru · 2022-03-31 11:14 WP, thanks for the response. I have tried to move my code to the sample program you supplied but your grid does not change it size when I try and anchor it to a larger panel. More work required but I am rather busy at the moment but I will work on it asap. However I chased back through some code I downloaded to investigate StringGrid sorts. The StringGrid sort project is attached. On the right of the grid is a blank space and click on this and the nearest cell is highlighted. Add AllowOutboundEvents in the FormCreate and set it false and no cells are highlighted. No exception. To me a case of clicking outside a grids and the the click being detected as part of the grid. At the moment I have no idea why my code would cause the exception. Ian ---------------------------------------------------------------------- Josh · 2022-03-31 11:50 hi simple test project, grid in panel, so you can see the col and row at position when mousedowd,mousemove and clicked, you can also enable/disable AllowOutboundEvents. i get no errors at all. suspect you have other code relying on the grid row col value, and no check is done for a -1 value, so maybe your getting a floating point error. in other code. ---------------------------------------------------------------------- wp · 2022-03-31 12:02 Quote from: iru on March 31, 2022, 01:14:56 pmI have tried to move my code to the sample program you supplied but your grid does not change it size when I try and anchor it to a larger panel. My grid is client-aligned to the form (StringGrid1.Align := alClient) so that it changes its size together with the form. If you want to control the grid's size independently you should set StringGrid1.Align := alNone. ---------------------------------------------------------------------- iru · 2022-04-01 04:55 I have chased this further in my code and found the problem (me) In the MouseDown code I had a test on the row number:     If 'row' <> 0 then.... This let the -1 row count through.... It should have been     If 'row' > 0 then... Thanks for the help. Ian