VB6 Tip: Give users more options by generating an auto-OK dialog box
Takeaway: Using this timed-response technique, you can give your users the option of changing program defaults or letting the program run with the established defaults.
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 = 5Dim TimeLeftAs Integer
Here's the form's Load event procedure:
Private Sub Form_Load()TimeLeft = AUTOCLOSE
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Here's the Timer event procedure:
Private Sub Timer1_Timer()TimeLeft = TimeLeft - 1
lblTimeRemaining.Caption = TimeLeft
If TimeLeft = 0 Then
Timer1.Enabled = False
Call OKButton_Click
End If
End Sub
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!
SponsoredWhite Papers, Webcasts, and Downloads
- The New Generation of Microsoft Certifications: What's at the Core? Global Knowledge
- Windows Server 2008 Hyper-V Global Knowledge
- SharePoint Customization Global Knowledge
- The OSI Model: Understanding the Seven Layers of Computer Networks Global Knowledge
- ITIL: What It Is and Why You Should Care Global Knowledge
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





