Friends,
I made a small calculator in a form.It works well. On the form there is an Exit Button to close the form.
Now I want this task.If I keep the calculator on the form (Computer) idle for 60 seconds the form must be closed automatically / If I goes on clicking some or other button on the calculator the form should not be closed (until 60 seconds completed after the last button click) Any body please help me. Thanks in advance

Dani AI

Generated

correctly pointed to the docs, but the actual bug in ’s snippet is that the counter never gets reset when a button is clicked. The timer simply keeps incrementing no matter what you do. The simplest fix is a module-level idle counter that the Timer increments and that every user-interaction handler resets to zero.

Example (VB6-style):

Private idleSeconds As Integer

Private Sub Form_Load()
    Timer1.Interval = 1000    ' 1 second
    Timer1.Enabled = True
    idleSeconds = 0
End Sub

Private Sub Timer1_Timer()
    idleSeconds = idleSeconds + 1
    If idleSeconds >= 60 Then Unload Me
End Sub

' In every calculator button (and other input handlers) add:
Private Sub cmdDigit_Click()
    idleSeconds = 0
    ' ... normal button work ...
End Sub

' Optionally reset on form mouse/keyboard to catch non-button input:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    idleSeconds = 0
End Sub

Use Unload Me (not End) to close just the form and release its resources; End will terminate the whole process. If you need system-wide idle detection (so inactivity outside the form is counted) use the Windows API GetLastInputInfo instead of a form timer; it reports the last input tick for the session. (learn.microsoft.com)

Troubleshooting tips: make sure Timer1.Enabled = True, use a 1-second interval for straightforward seconds counting (or adjust logic if you use 500 ms), and add idleSeconds = 0 to every place the user can interact. If the form has many controls, centralize the reset in small helper routine you call from each handler to avoid missed cases.

Recommended Answers

All 2 Replies

Thank you..But I tried like this

Private Sub Timer1_Timer()
Static i As Integer
  If i = 20 Then Exiting
  i = i + 1
End Sub

The interval of the timer is 500. Ok it works only when the form is opened and kept idle for more than 5 seconds(without clicking any buttons). If we click any button ,and even if we keep the computer idle for a long time the form is not exited.I want the form to exit after 5 seconds after the last button click.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.