1. Computing

Create a Time Out Button / Entry Form in Delphi

By

Create a Time Out Button / Entry Form in Delphi

If you are developing a "quiz-like" type of application, or an "exam-like" application, a user needs to make a selection and click the "Next" button, to go to the next "question".
In such scenarios you might need to force a user to make a selection (choose the "answer") within a specified time interval.

You, as a developer, should include some code that will programmatically fire the OnClick event of the "Next" button, when the time has elapsed. Here's how:

The Auto Click Button / Entry Form

Let's build a simple Delphi form hosting a RadioButton component providing a user with the possible answers. The form will also display a progress bar to let the user know how much time he has to make a decision. The progress bar will be updated inside an OnTimer event of a TTimer component. When the time elapses, the "Next" button is "clicked" auto-magically.

Here's how to create a 3-seconds auto-submit entry form:

  1. Drop a TTimer, TProgressBar, TRadioGroup and TButton on a form. Leave the default names (for example: "Button1" for button)
  2. Add several items (possible answers) to the radio group.
  3. Setup the timer and the progress bar in the Form's OnCreate event:
     procedure TTimeOutForm.FormCreate(Sender: TObject) ;
     begin
        Timer1.Interval := 10;
        ProgressBar1.Max := 300; //300 * 10 = 3000 := 3 seconds
        ProgressBar1.Position := ProgressBar1.Max;
     
        Timer1.Enabled := true;
     end; 
    We set the Interval property of the Timer1 (TTimer) component to 10 milliseconds. The progress bar Max property is set to 300.
  4. The OnTime event of the Timer1 component will fire every 10 milliseconds - decrementing the position of the progress bar.
     procedure TTimeOutForm.Timer1Timer(Sender: TObject) ;
     begin
        ProgressBar1.StepBy(-1) ;
        if ProgressBar1.Position = 0 then
        begin
          //auto - click
          Button1Click(Timer1) ;
        end;
     end; 
    After 3 seconds, when the Position property reaches the zero value, we fire the OnClick event of the "submit" button programmatically:
  5. When the user clicks the submit ("Next") button, we'll display the selected item from the radio group. If the OnClick was fired due to "time-elapsed" situation, the Sender parameter will point to "Timer1":
     procedure TTimeOutForm.Button1Click(Sender: TObject) ;
     var
        selected : string;
     begin
        //stop timer
        Timer1.Enabled := false;
     
        //do we have a selection, if so what is it...
        if RadioGroup1.ItemIndex = -1 then
        begin
          selected := 'NOTHING SELECTED';
        end
        else
        begin
          selected := RadioGroup1.Items[RadioGroup1.ItemIndex];
        end;
     
        //was the OnClick event fired by Timer?
        if Sender is TTimer then
        begin
          ShowMessage('Time expired, selected :' + selected) ;
        end
        else //Sender is Button
        begin
          ShowMessage('User selected :' + selected) ;
        end;
     end; 
That's all! An example of a "timeout-submit" entry form.

Note: take a look at how to place a progress bar inside a standard dialog box and the role of the Sender parameter in event handlers.

©2014 About.com. All rights reserved.