Recent

Author Topic: Problem with selected row in Tstringgrid  (Read 5190 times)

jeremzw

  • Newbie
  • Posts: 3
Problem with selected row in Tstringgrid
« on: May 22, 2020, 12:25:29 pm »
Hello,

I'm having a problem with the row selection in tstringrid :
My grid is on goRowSelect (single row mode).

After selecting a row for the first time, moving the mouse over the stringgrid automatically select another row (the one the mouse is over), as a kind of magnetic effect, until I click another row.
Then, clicking a third row will start the same effect again, until I click a 4th row, and so on...

Is there a way to completely get rid of the rows being selected by just moving the mouse ?
I can't find the option in the object inspector...

Thanks

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problem with selected row in Tstringgrid
« Reply #1 on: May 22, 2020, 01:05:38 pm »
turn goRangeSelect  off ?
The only true wisdom is knowing you know nothing

jeremzw

  • Newbie
  • Posts: 3
Re: Problem with selected row in Tstringgrid
« Reply #2 on: May 22, 2020, 01:30:48 pm »
It's already at false...

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Problem with selected row in Tstringgrid
« Reply #3 on: May 22, 2020, 01:43:19 pm »
I don't know, there are so many options...

For a test, drop another StringGrid on the form, and compare which values in the Options are different. Reset the Options of your probematic grid to those of the temp grid. Does the issue go away?

Another possibility: Is there a handler for OnPrepareCanvas or OnDrawCell? Their code could reult in the same effect.

jeremzw

  • Newbie
  • Posts: 3
Re: Problem with selected row in Tstringgrid
« Reply #4 on: May 22, 2020, 02:11:12 pm »
Thanks, I did your test, but I couldn't find any specific options that could be wrong...
Here's a capture of the object inspector.

No handler. There's only one event onselection.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problem with selected row in Tstringgrid
« Reply #5 on: May 22, 2020, 04:46:26 pm »
seems to be different here. I turn off rangeSelect and I only get one row, It does not expand to more rows if you drag the mouse.

 if you are getting this without dragging the mouse then you have some other issue do doubt.

 What target widget are you on ? I did this on Windblows


The only true wisdom is knowing you know nothing

Eleazar

  • New Member
  • *
  • Posts: 26
Re: Problem with selected row in Tstringgrid
« Reply #6 on: October 21, 2020, 02:06:39 am »
Yes, I encountered the same problem...Took me a few hours to find solution. Problem with OnSelection is that it already starts actions on mousedown. When you have some time consuming procedures it seems to get messed up. I noticed when you pause long enough between mousedown and mouseup, the problem goes away. But that is not how a normal user clicks. So we need a solution....

 My solution was to use the MouseUp event and  translate X,Y to a row number with aStringrid.MouseToCell(X, Y, Col, Row), then execute the actions. (declare variables Col, Row before using the procedure).
Remember you also have to do this for KeyUp, because when not using OnSelection this also needs a handler. Just call your action with aStringrid.Row, that variable holds the selected row.   
« Last Edit: October 21, 2020, 02:13:45 am by Eleazar »

jcmontherock

  • Full Member
  • ***
  • Posts: 234
Re: Problem with selected row in Tstringgrid
« Reply #7 on: October 31, 2020, 11:43:23 am »
I have the same problem. Unfortunately the Eleazar's solution is not applicable because I need to have the following options: goRowSelect and rsmMulti for RangeSelect mode.
One more thing: the first row (after the fixed row) is automatically selected without selection event fired.
Windows 11 UTF8-64 - Lazarus 3.2-64 - FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Problem with selected row in Tstringgrid
« Reply #8 on: October 31, 2020, 01:11:15 pm »
I cannot reproduce this issue on Windows. Here's what I did:
- I added a new StringGrid to the form
- Set its RowCount to 20 and its ColCount to 10
- Activate the options goEditing and goRowSelect.

When I run the program and press the left mouse button on some cell, the corresponding row gets highlighted. When I draw the mouse (with left button down) across the  grid, all the rows between the current mouse position and the initial Mouse-down position are highlighted. I assume that this the situation reported in the first post.

Now I deactivate the option goRangeSelect, run the program again and do the same:
- Click on some cell --> the entire row gets highlighted
- Drag across the grid over other rows. Now only the row underneath the mouse is highlighted.

So, for me, this is absolutely correct behaviour. If you cannot confirm this please specify your operating system (plus widgetset in case of Linux), as well as Lazarus and fpc versions (with bitness).

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problem with selected row in Tstringgrid
« Reply #9 on: October 31, 2020, 02:50:34 pm »
It may have something to do with the user's code base using some of the events that take place while selecting the cells..

 if you try this for example
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1SelectCell(Sender: TObject; aCol, aRow: Integer;
  2.   var CanSelect: Boolean);
  3. begin
  4.   caption := double(now).tostring;
  5. end;                              
  6.  

while selecting cells or just moving the mouse within a cell with mouse down, this event gets called on each mouse move while it has already been called before.

 this can cause a pileup depending on what is happening with the code within this action.

 wouldn't it be more prudent to log the last cell that called the OnSelectCell on the mouse move so it don't keep calling the same cell over and over ?
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Problem with selected row in Tstringgrid
« Reply #10 on: October 31, 2020, 03:24:25 pm »
No handler. There's only one event onselection.
As jamie writes, this is may contribute to the problem (though OnSelection is distinct from OnSelectCell, which he references).
Selection and cell selection events come thick and fast in a working grid, and so handlers should be as short and fast as possible.
If you can achieve your goal without resort to a grid selection event, it may make your life easier.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Problem with selected row in Tstringgrid
« Reply #11 on: October 31, 2020, 03:32:56 pm »
there are a a lot of things built around this and I am wondering if maybe a trap should be installed for the SelectCell method via an option of course to track the last select cell and have it not called every time.

 There are many controls based from this like the TDBGrid for example and I wonder how that is handled in a case like this ?
The only true wisdom is knowing you know nothing

dimer

  • Newbie
  • Posts: 5
[Solved] Problem with selected row in Tstringgrid
« Reply #12 on: August 11, 2021, 12:18:31 am »
It seems to me you're waiting something wrong from TStringGrid.OnSelection; this event doesn't match a standard reaction of grid (in interactive mode).

OnSelection should process events of changing selected row, cell or so on. Only if this row or cell is selected, and when it is selected, just in moment! But if you want to react an operator's action, you must use TStringGrid.OnClick. In this event you're well knowing the momentary values of Row and Col properties - use them in a procedure, it is enough.

OnSelection has a bug (or feature) with non-processing keyboard or mouse event up to it's closing, so after OnSelection you have a selected cell or row in a grid for futher actions... but you're not programmed these actions. And your cursor is migrating within grid while you're moving a mouse... In OnClick all the events of keyboard or mouse are procedding up to the end, and the corresponding message will be killed (guaranteed).

Change your algorythms... and you're get a needed solution.

[Update] Pay your attention to property TStringGrid.MouseWheelOption! Set this value to mvGrid, and you'll get the desired grid scrolling behaviour.
« Last Edit: August 11, 2021, 01:47:40 pm by dimer »

 

TinyPortal © 2005-2018