1. Home
  2. Computing & Technology
  3. Delphi Programming

How to set the progress bar position using mouse

By Zarko Gajic, About.com

By default, the TProgressBar control is used to provide visual feedback about the progress of a procedure within an application. Programmatically, a developer can set Min and Max properties to specify the lower and the upper limit of the range of possible positions. The Position property (or the Step, StepBy methods) is then used to set the current position of the progress bar.

If you want to enable the users of you application to set the position of the progress bar visually, using mouse, you can use the next trick:

//ProgressBar1 OnMouseMove
procedure TForm1.ProgressBar1MouseMove(
   Sender: TObject;
   Shift: TShiftState;
   X, Y: Integer) ;
var
   newPosition : integer;
begin
   if ssShift in Shift then
   begin
     ProgressBar1.Cursor := crHSplit;
     newPosition := Round(x * ProgressBar1.Max / ProgressBar1.ClientWidth) ;
     ProgressBar1.Position := newPosition;
   end
   else
   begin

     ProgressBar1.Cursor := crDefault;
   end;
end;

The OnMouseMove event for the TProgressBar component named ProgressBar1 is handled, when the mouse is over the ProgressBar control, and the Shift key is pressed, a user can dynamically change the value of the Position property.
Note: the code above assumes that "pbHorizontal" is set for the Orientation property of the ProgressBar - the "X" parameter is used to calculate the new Position value. If "pbVertical" is specified, the "Y" parameter should be used.

Delphi tips navigator:
» How to remeber Delphi form controls position and size
« UDP Makes a Difference (UDP vs. TCP)

More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TMouse
  6. Set the Progress Bar Position Using Mouse in Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.