Board index » delphi » I only want an onchange event of ttrackbar when the mouse is released

I only want an onchange event of ttrackbar when the mouse is released

I want to have an onchange event handler for the ttrackbar control, however,
I only want to process this event once the mouse button is released (not
while the slider is being moved).

What is the best way to accomplish this? Can anyone provide an example?

 

Re:I only want an onchange event of ttrackbar when the mouse is released


Quote
Rob wrote...
> I want to have an onchange event handler for the ttrackbar control, however,
> I only want to process this event once the mouse button is released (not
> while the slider is being moved).

> What is the best way to accomplish this? Can anyone provide an example?

Not sure if the code below is the best way but it does seems to work.
Note: - when you're only using this you will not be notified
when the user changes the trackbar by using the keyboard.

Another idea to prevent updating to often would be to (re)start
a timer (e.g. 1 sec) on every onchange and do the actually
updating in the ontimer event (and disable the timer again).

--
Pieter

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TMyTrackBar = class(TTrackbar)
  published
    property OnMouseDown;
    property OnMouseUp;
  end;

  TForm1 = class(TForm)
    procedure MyTrackBarMouseUp(Sender: TObject; Button: TMouseButton; Shift:
TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FMyTrackBar : TMyTrackBar;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.MyTrackBarMouseUp(Sender: TObject; Button: TMouseButton; Shift:
TShiftState; X, Y: Integer);
begin
  MessageDlg('Mouse up on position ' + IntToStr(FMyTrackBar.Position),
             mtInformation, [mbOk], 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FMyTrackBar := TMyTrackBar.Create(Self);
  with FMyTrackBar do
  begin
    Parent := Self;
    Left := 10;
    Top := 10;
    OnMouseUp := MyTrackBarMouseUp;
  end;
end;

end.

Re:I only want an onchange event of ttrackbar when the mouse is released


Thanks for the solution...

I ended up creating a descendant ttrackbar and publishing the mouseup and
mousedown events.

Quote
"Pieter Zijlstra" <pzijlstr...@freeler.nl> wrote in message

news:8n6s08$deo5@bornews.borland.com...
Quote
> Rob wrote...
> > I want to have an onchange event handler for the ttrackbar control,
however,
> > I only want to process this event once the mouse button is released (not
> > while the slider is being moved).

> > What is the best way to accomplish this? Can anyone provide an example?

> Not sure if the code below is the best way but it does seems to work.
> Note: - when you're only using this you will not be notified
> when the user changes the trackbar by using the keyboard.

> Another idea to prevent updating to often would be to (re)start
> a timer (e.g. 1 sec) on every onchange and do the actually
> updating in the ontimer event (and disable the timer again).

> --
> Pieter

> interface

> uses
>   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
>   Dialogs, StdCtrls, ComCtrls;

> type
>   TMyTrackBar = class(TTrackbar)
>   published
>     property OnMouseDown;
>     property OnMouseUp;
>   end;

>   TForm1 = class(TForm)
>     procedure MyTrackBarMouseUp(Sender: TObject; Button: TMouseButton;
Shift:
> TShiftState; X, Y: Integer);
>     procedure FormCreate(Sender: TObject);
>   private
>     { Private declarations }
>     FMyTrackBar : TMyTrackBar;
>   public
>     { Public declarations }
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> procedure TForm1.MyTrackBarMouseUp(Sender: TObject; Button: TMouseButton;
Shift:
> TShiftState; X, Y: Integer);
> begin
>   MessageDlg('Mouse up on position ' + IntToStr(FMyTrackBar.Position),
>              mtInformation, [mbOk], 0);
> end;

> procedure TForm1.FormCreate(Sender: TObject);
> begin
>   FMyTrackBar := TMyTrackBar.Create(Self);
>   with FMyTrackBar do
>   begin
>     Parent := Self;
>     Left := 10;
>     Top := 10;
>     OnMouseUp := MyTrackBarMouseUp;
>   end;
> end;

> end.

Other Threads