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

How to place a progress bar inside a standard dialog box

By , About.com Guide

Let's say you have a standard Windows dialog box displaying a question to the user with "Yes" and "No" buttons. Wouldn't it be great if a progress bar could be displayed within a dialog box "counting" seconds until the dialog box automatically closes itself?

  1. We first create a dialog using CreateMessageDialog
  2. This function will return a form object with dialog
  3. In this object we can add a ProgressBar
  4. We also add a Timer object for dynamic progress bar position update
  5. Show dialog using ShowModal
  6. Handle the OnTimer event of the TTimer component to see if the elapsed number of seconds has passed - if so, we close the dialog by setting the ModalResult property, from code, to mrCancel.
  7. If not, we use StepIt to update the progressbar.

Drop a TButton (Button1) on a form (Form1), and try the next code:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.Button1Click(Sender: TObject) ;
var
   AMsgDialog : TForm;
   AProgressBar : TProgressBar;
   ATimer : TTimer;
begin
   AMsgDialog := CreateMessageDialog('Quickly! Answer Yes or No!', mtWarning, [mbYes, mbNo]) ;
   AProgressBar := TProgressBar.Create(AMsgDialog) ;
   ATimer := TTimer.Create(AMsgDialog) ;
   with AMsgDialog do
   try
    Tag := 10; //seconds!

    Caption := 'You have 10 seconds';
    Height := 150;

    with AProgressBar do begin
     Name := 'Progress';
     Parent := AMsgDialog;
     Max := AMsgDialog.Tag; //seconds
     Step := 1;
     Top := 100;
     Left := 8;
     Width := AMsgDialog.ClientWidth - 16;
    end;

    with ATimer do
    begin
     Interval := 1000;
     OnTimer:=DialogTimer;
    end;

    case ShowModal of
     ID_YES: ShowMessage('Answered "Yes".') ;
     ID_NO: ShowMessage('Answered "No".') ;
     ID_CANCEL: ShowMessage('Time up!')
    end;//case
   finally
    ATimer.OnTimer := nil;
    Free;
   end;
end;


//make sure you add this function's header in the private part of the TForm1 type declaration.
procedure TForm1.DialogTimer(Sender: TObject) ;
var
   aPB : TProgressBar;
begin
   if NOT (Sender is TTimer) then Exit;

   if ((Sender as TTimer).Owner) is TForm then
   with ((Sender as TTimer).Owner) as TForm do
   begin
     aPB := TProgressBar(FindComponent('Progress')) ;

     if aPB.Position >= aPB.Max then
       ModalResult := mrCancel
     else
       aPB.StepIt;
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
Note: you'll also want to look at

Delphi tips navigator:
» Enter key or Return key?
« TIPS

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TProgressBar
  6. How to place a progress bar inside a standard dialog box

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

All rights reserved.