Recent

Author Topic: TrackBar OnChange event  (Read 12873 times)

Davo

  • Full Member
  • ***
  • Posts: 141
TrackBar OnChange event
« on: June 17, 2014, 04:03:46 pm »
I've noticed that when a TrackBar button is moved the TrackBar's OnChange event is triggered three times in succession. For example if a TrackBar has min and max settings of 0 and 10 respectively and a breakpoint is set on the following single line of code in its OnChange event handler, when the button is just nudged so that its position changes by just 1 the breakpoint is triggered three times.

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  label1.caption := IntToStr(TrackBar1.position);
end;

Is this normal behaviour? Is there a setting that I've missed that ensures that the OnChange event handler is triggered only once? Or do I have to resort to using boolean flags in the code to skip the action in the OnChange event handler for the second and third visits there?

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: TrackBar OnChange event
« Reply #1 on: June 17, 2014, 04:29:00 pm »
I'm on Windows - I'll confirm tonight, but if you're correct, this would explain an oddity I've been observing in one of my apps.   

What OS and widgetset are you using?
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Davo

  • Full Member
  • ***
  • Posts: 141
Re: TrackBar OnChange event
« Reply #2 on: June 17, 2014, 04:42:26 pm »
Win7 Home Premium

Lazarus version 1.0.10
FPC version 2.6.2
SVN Revision 41613
i386-win32-win32/win64

Running on a 64 bit computer

If I knew how to determine the widgetset I'd let you know.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: TrackBar OnChange event
« Reply #3 on: June 17, 2014, 04:49:30 pm »
Ah, you'll be using win32/win64 widgetset then, same as me.  Right, I'll definitely check this out tonight...

FYI: You set the Widgetset for your project in Project Options.  You set the Widgetset for Lazarus in Tools - Configure "Build Lazarus".  I *think* the two are unrelated, haven't done much work switching between Widgetsets...

Update:  I also think the string "i386-win32-win32/win64" is TargetCPU-Widgetset :-)
« Last Edit: June 17, 2014, 04:51:20 pm by Mike.Cornflake »
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: TrackBar OnChange event
« Reply #4 on: June 17, 2014, 04:49:53 pm »
I can confirm with Wine + Laz. 1.2.0.
One OnChange is triggered when knob moves and two additional OnChange on MouseUp.

Quote
Is this normal behaviour? Is there a setting that I've missed that ensures that the OnChange event handler is triggered only once? Or do I have to resort to using boolean flags in the code to skip the action in the OnChange event handler for the second and third visits there?

You should open a bugreport, category Widgetset-Win32.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Davo

  • Full Member
  • ***
  • Posts: 141
Re: TrackBar OnChange event
« Reply #5 on: June 18, 2014, 12:00:24 am »
Project Options | Inherited | LCL 1.0.10 | LCL Widget Type = “win/32”
Tools | Configure “Build Lazarus” | LCL widget type | win32/win64

Both settings are the default values obtained on downloading Lazarus.

Exploring further and a]ltering the code to :

Code: [Select]
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  showmessage('Change');
  label1.caption := inttostr(TrackBar1.position);
end;

procedure TForm1.TrackBar1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  showmessage('Mouseup');
end;

gives the following result :

If the trackbar button is clicked without releasing the mouse button nothing happens. Which is logical.

If the trackbar button is clicked without moving it along the bar the message “Change” is displayed three times in succession and then the message “Mouseup” is shown (it is necessary to click the message box's OK button each time).

If the trackbar button is moved along the bar the message “Change” is displayed three times in succession but not the “Mouseup”message even 'though a MouseUp event must have occurred because the mouse was moved from the trackbar to click the OK button in the message box.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: TrackBar OnChange event
« Reply #6 on: June 18, 2014, 07:24:08 am »
I confirmed this on my system as well, but this wasn't the source of my oddity.

For now, I've implemented the following in my onchange

Code: [Select]
If Trackbar1.Position<>FLastPosition Then
Begin
  Try 
     ... Code ...
  Finally
    FLastPosition := Trackbar1.Position;
  End; 
End;

and in my OnCreate I initialise FLastPosition to -1.

Could you please raise a bug report on this item?   If you don't get a chance today, I'll do one tonight...
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Davo

  • Full Member
  • ***
  • Posts: 141
Re: TrackBar OnChange event
« Reply #7 on: June 18, 2014, 11:51:16 am »
Mike. Thanks for your confirmations. Your code fix works well in the small application that I'm currently building – thanks again for that. I won't get a chance to submit a bug report within the next 24 hours so, if you don't mind, can you submit it? And anyway I'm sure that you will do a better job of it than I can.

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: TrackBar OnChange event
« Reply #8 on: June 18, 2014, 12:55:05 pm »
Quote
Your code fix works well in the small application that I'm currently building
Sweet :-)

Quote
if you don't mind, can you submit it?
No problems - I'll get onto it tonight :-)
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: TrackBar OnChange event
« Reply #9 on: June 18, 2014, 06:15:58 pm »
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1272
Re: TrackBar OnChange event
« Reply #10 on: October 07, 2014, 05:56:21 pm »
Patch applied.  Will be 1.2.6 & is currently in Trunk.
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

 

TinyPortal © 2005-2018