-
Jul 4th, 2013, 08:24 PM
#1
Thread Starter
New Member
Timer Test
I've programmed in Basic in the past but I'm brand new to VB. I'm trying to understand the timer function. I wrote a short program to display a count from 1 to 20 in one second intervals or so I thought.
Here's the code:
Dim Counter As Integer
Counter = 1
Do
Timer1.Enabled = False
TextBox1.Text = Counter
Timer1.Enabled = True
Counter = Counter + 1
Loop While Counter < 21
End
I put this under a Button_Click event with the Timer_Tick listed first. The Timer_Tick has no code associated with it and the interval is 1000. When I run the progam, it stops in less than a second and ends and the form goes back to design mode.
I've also tried For-Next and If statements to increment the counter but nothing works. What am I doing wrong?
Thanks for the help.
-
Jul 5th, 2013, 06:58 AM
#2
New Member
Re: Timer Test
You aren't. If timer don't have any code so what are you testing?
Timer works this way:
If timer is enabled, each milisecond (interval on the properties), run the code that is on timer function
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'*your code here will run here
End Sub
You don't need to make that while, i don't know why you are trying to do.
Just make this simple program:
Code:
Public Class Form1
Dim counter As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
counter += 1
TextBox1.Text = counter
End Sub
End Class
-
Jul 5th, 2013, 08:03 AM
#3
Thread Starter
New Member
Re: Timer Test
Thanks for the help. Sorry I didn't fully explain my problem. What I'm trying to do is start a counter with a button click that displays 1 to 20 in one second intervals and then stops. The Loop While statement stops the counter at twenty. I haven't run your example program but it has no button click event or test to stop counting. I'll play with it to see if I can get it to work the way I want.
Also, my program is an example of the way "Basic" would do it. The += operator is much simpler.
Thanks again.
-
Jul 5th, 2013, 08:12 AM
#4
Re: Timer Test
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
counter = 0
Timer1.Interval = 1000 'one sec. in ms.
Timer1.Enabled = True
End Sub
Dim counter As Integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If counter >= 20 Then
Timer1.Enabled = False
Else
counter += 1
TextBox1.Text = counter.ToString
End If
End Sub
-
Jul 5th, 2013, 08:28 AM
#5
New Member
Re: Timer Test
but thats pointless xD
You can do that without timers etc.
edit: OH WAIT I GOT IT XD, see the dbasnett code
Last edited by b mitsai; Jul 5th, 2013 at 08:34 AM.
-
Jul 5th, 2013, 10:06 AM
#6
Re: Timer Test
It should be pointed out that this method should not be used as timing. In other words the actual elapsed time will not be 20 seconds when the counter reaches 20. If we add a stopwatch to the code we can see that.
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
counter = 0
Timer1.Interval = 1000 'one sec. in ms.
stpw.Reset()
stpw.Start()
Timer1.Enabled = True
End Sub
Dim counter As Integer = 0
Dim stpw As New Stopwatch
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If counter >= 20 Then
stpw.Stop()
Debug.WriteLine(stpw.Elapsed)
Timer1.Enabled = False
Else
counter += 1
TextBox1.Text = counter.ToString
End If
End Sub
-
Jul 5th, 2013, 10:28 AM
#7
Thread Starter
New Member
Re: Timer Test
Thanks much dbasnett. More info for my programming journey. I was wondering if "counter" needed to be a string or an integer to display. I ran the program both ways and it did fine. Not sure why. Also, placing the Dim statement between Subs evidently works globally for both. I also found out that the listing sequence of events is important.
My reason for learning VB is to write a program to monitor my AC usage via an Arduino. I want to feed the data to Excel and graph it. This timer exercise is part of that. I also need to explore the TimeString function. I have more learning ahead and a long way to go.
Thanks again.
-
Jul 5th, 2013, 11:37 AM
#8
Re: Timer Test
It is a good habit to add
Option Strict On
as the first line of your program, or make it the default setting in the IDE.
-
Jul 5th, 2013, 02:17 PM
#9
Thread Starter
New Member
Re: Timer Test
I'll do that. I didn't know the stpw command existed. It's not listed in "Visual Basic Step by Step" that I'm using. Is there a reference out there that lists all the commands and functions for VB and what they do? It would be helpful to have that info available.
Thanks.
-
Jul 5th, 2013, 03:06 PM
#10
-
Jul 6th, 2013, 03:38 PM
#11
Thread Starter
New Member
Re: Timer Test
So, I ran this a number of times and saw no difference in the result. I assume the Debug.WriteLine statement is supposed to display the difference between the timer count and the stopwatch count but the program always stops after 20 is reached and nothing else happens. Am I doing something wrong?
-
Jul 6th, 2013, 04:27 PM
#12
Re: Timer Test
It was to show that more than 20 seconds had elapsed. Some would think that if the timer fired every 1000 ms. 20 times that 20 seconds would have elapsed. Because of the way the timer is handled this is not true, which is what the test tried to show.
-
Jul 6th, 2013, 06:10 PM
#13
Thread Starter
New Member
Re: Timer Test
Thanks, I understood that and I think I know why the difference. However, I'm curious as to why the difference didn't display. If you don't want to go any farther with this, I understand. You have other things to do.
Thanks again.
-
Jul 7th, 2013, 09:30 AM
#14
Re: Timer Test
Try changing this line
Code:
Debug.WriteLine(stpw.Elapsed)
to
Code:
TextBox1.Text = stpw.Elapsed.ToString
The actual elapsed time was showing in the debug output window of the IDE. It would surprise me if it was 20.
Tags for this Thread
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
|