Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Grid - delayed OnRowChanged
Sat, Oct 21 2017 12:02 PMPermanent Link

thomh

Hi,

I have a parent grid with an OnRowChanged property which performs a server request for child records when the row changes.

When I hold down the up/down keys or scroll fast in the grid, I do not want the OnRowChanged event to fire until I release the key or pause for X milliseconds to avoid unnecessary server request calls.

Would it be possible to have a DelayedRowChangeInterval property that you could set to delay the event firing?

Or is there another way to handle this?

Thanks.

// Thom
Sat, Oct 21 2017 2:59 PMPermanent Link

Mark Brooks

Slikware

Avatar

Have you thought about maybe using a TTimer to handle this?
Mon, Oct 23 2017 3:54 AMPermanent Link

Matthew Jones

Mark Brooks wrote:

> Have you thought about maybe using a TTimer to handle this?

To expand on this, it is indeed exactly what I do. I brought up the idea of "delay" being built in on another aspect of events, and in typing came to the conclusion that it is better for the component to immediately inform, and then we as developers can choose what to do with that event, either acting immediately, or adding a delay.

The delay is simple - have a timer running at a fairly fast interval. Then in the event, set a boolean flag and the time that the action (lookup) should happen. In the timer, check if the flag is set, and if so, check if the time has passed. And if so, do the lookup.

procedure TfrmBlah.cboCategoryChange(Sender: TObject);
begin
   if not m_bLoaded then
       exit;
   m_bLookupNeeded := true;
   m_tmDoLookupAt := IncMilliSecond(Now, cboCategory.KeyPressInterval);
   TimerComboComplete.Enabled := true;
end;


procedure TfrmBlah.TimerComboCompleteTimer(Sender: TObject);
begin
   if Now > m_tmDoLookupAt then
   begin
       TimerComboComplete.Enabled := false;
       DoItemLookup;
   end;
end;

IncMilliSecond came from code posted here by other helpful people IIRC.

I am assuming that enabling the timer multiple times is not resetting the clock - perhaps I should check that, but it works anyway!

--

Matthew Jones
Mon, Oct 23 2017 12:14 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Thom,

<< Would it be possible to have a DelayedRowChangeInterval property that you could set to delay the event firing? >>

I can't really delay the firing of the event, but...

<< Or is there another way to handle this? >>

Yes, there is.  What you're trying to do is called de-bouncing and, as others have pointed out, the solution is to use a TTimer to achieve the result that you want.  The general idea is that you disable and then re-enable the timer (the timer is initially disabled) whenever the event fires, and have the timer's OnTimer event trigger the code that you want to execute in a delayed fashion.

However, you *do* need to be careful with this type of setup because you could get into a situation where the dataset, or some property of the dataset, has been freed or changed between the time that the OnRowChanged event has fired and the OnTimer (your code) is executed.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Oct 23 2017 3:55 PMPermanent Link

thomh

Thanks, guys. Lots of good info here to get me started.

// Thom
Image