-
Aug 12th, 2018, 04:59 AM
#1
Thread Starter
Addicted Member
[RESOLVED] How to use the timer control
Hello everyone,
I am trying to do a simple task but I can't seem to handle it and I don't understand why. I need to show form for 10 seconds when the user is not allowed to press the OK button.
The OK Button has to be disabled for 10 seconds and I want to show in the text button the time elapsed (for example Ok (10), Ok(9), Ok(8), etc...)
I added a timer control and this is the code I used:
In the Load event of the form:
Code:
Dim counter as Integer = 0
timer1.Interval = 5000
timer1.Enabled = True
bttnOk.Enabled=False
timer1.Start()
bttnOk.Enabled=True
and then in the timer_tick event
Code:
Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
counter += 1
seconds=0
If counter = 1000 Then
counter = 0
seconds+=1
bttnOk.Text = "Ok" & "(" & seconds & ")"
End If
End Sub
why doesn't this work as expected?
-
Aug 12th, 2018, 05:22 AM
#2
Re: How to use the timer control
There are a few issues there... the first is that you enable the button in the Load event, and that code will keep on running even tho you have started the timer (because the timer runs independently). You should enable the button in the Tick event instead (when the count is high enough).
Another issue is that you have declared a variable in the Load event, but seem to be trying to use the same variable in the Tick event, and you cannot do that - a variable declared inside a routine (Sub or Function etc) only exists inside that routine. If you want a variable to be available between routines you can pass it as a parameter (which is not appropriate here), or declare it at a higher level (such as at the top of the class, outside of any routines).
Something else is that you have set the timers Interval to 5000 (5 seconds), and then inside the Tick event increment a counter, and wait until it reaches 1000 before you increase the seconds variable (which you actually want to decrease). In order for the text of the button to change, you will have to wait for 5*1000 seconds.
The code should be something like this:
Code:
...
'in the Load event
timer1.Interval = 1000 '1 second
seconds = 10
bttnOk.Enabled = False
timer1.Start() 'this is the same as timer1.Enabled = True
...
Private seconds as Integer 'this variable will be available in both routines
Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
'as the Interval is 1 second, the code here will run once a second - there is no need for any extra code to make that happen
seconds -= 1
bttnOk.Text = "Ok (" & seconds & ")"
If seconds <= 0 Then
bttnOk.Enabled = True
timer1.Stop() 'stop the timer when done (otherwise it will keep on changing the button text)
End If
End Sub
-
Aug 12th, 2018, 09:26 AM
#3
Thread Starter
Addicted Member
Re: How to use the timer control
Yes. It works now. It seems I didn't quite understand how timer really works in the background. Now it's clear for me.
Thank you.
-
Aug 12th, 2018, 10:12 AM
#4
Re: [RESOLVED] How to use the timer control
For the record, the Timer is a component, not a control. Any component can be added to the Toolbox and added to a form in the designer because its the Component class that provides the designer support. A control is a specialised component because the Control class inherits the Component class and adds the actual UI support.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|