With some programs, it may be desirable to let them run unattended. But what if you want to give the user the option of changing program defaults, or letting the program run on its own using the defaults?
One way to do this is to use what I call auto-OK dialog boxes. As the program runs, the dialog box pops up to let the user change options if desired. If the user doesn't respond within a certain time, the dialog box closes itself and the program continues.
I'll demonstrate how you can create this kind of dialog box. This technique is based on the Timer control and is accomplished this way:
- Place a timer on the form with its Interval property set to 1000 (1 second).
- In the form's Load event procedure, initialize a Time Remaining variable to the total number of seconds the dialog box should remain open. At the same time, set the Timer control's Enabled property to True so that it starts running.
- In the Timer control's Timer event procedure, decrement the Time Remaining variable by 1. Optionally, you can display the time remaining on the dialog box.
- When the Time Remaining variable reaches zero, call the Click event procedure for the form's OK button or equivalent, closing the form and continuing program execution.
Here's some sample code. At the form level, declare the following:Const AUTOCLOSE = 5
Dim TimeLeftAs Integer
Here's the form's Load event procedure:Private Sub Form_Load()
TimeLeft = AUTOCLOSE
Timer1.Interval = 1000
Timer1.Enabled = True
Here's the Timer event procedure:Private Sub Timer1_Timer()
TimeLeft = TimeLeft - 1
lblTimeRemaining.Caption = TimeLeft
If TimeLeft = 0 Then
Timer1.Enabled = False
In this example, the form's OK button is named OKButton. The code in the OK Button's event procedure, which isn't included here, will take whatever actions are required to close the dialog box and continue program execution.
An optional feature would be to use the Change event procedure of any controls on the form to turn the timer off. This cancels the countdown if the user makes any changes to the options that are presented on the form.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!