Page 1 of 1

VB Timer This tutorial shows you how to code a simple timer. Rate Topic: ***-- 2 Votes

#1 benpap  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 47
  • Joined: 24-November 07

Posted 06 December 2007 - 12:15 PM

Hi, I'm presenting a tutorials for begginers which shows you how to create your own timer.

We are going to use 5 components, also called objects.
  • a label as a title (lbltitle)
  • a text box (txtuser) where the user enters a the time he wants to countdown in seconds
  • another label (lblremain) where the remaining time is displayed
  • a timer (tmr) which is the "heart" of the application
  • a comand button (cmdstart) which starts the countdown

The 1st label (lbltitle) just tells the user to enter the user the time he wants to countdown. Put as a caption in its properties something like "Enter time to countdown.

Leave the text box (txtuser) blank and do the same to the second label (lblremain).

Use the following code for the command button (cmdstart)

Private Sub cmdstart_Click()
If Not IsNumeric(txtuser.Text) Then
MsgBox "Enter a value."
Else
	 tmr.Enabled = True
	 tmr.Interval = 1000
	 lblremain.Caption = txtuser.Text
End If
End Sub



I use an if statement which says:
If the text in the text box (txtuser) is not a number then a message box shall appear telling to enter a value.
If the previous statement is not true then it will enabel the timer (tmr) and it will change the Invertal of the timer (tmr) to 1000 (1 second) and will also change the caption of the remainig time to the same value as the user has entered in the text box (txtuser). Don't forget to end the if command.


Use the following code for the timer component (tmr):
Private Sub tmr_Timer()
	 If lblremain.Caption = 0 Then
		  tmr.Enabled = False
		  MsgBox Text1.Text & " sec are up!", vbInformation
	 Else
		  lblremain.Caption = lblremain.Caption - 1
	 End If
End Sub



This if statement says: If the caption (the characters) in the label (lblremain) eqals to zero (0) then the timer stops. Aftre that a meggasge box appears and stays that the values the user had entered in the text box (txtuser) is over. If the previous statement is false then it will remove one from the label. Because the timer is set to repeat its task every second it will do that until the value reaches zero.

Is This A Good Question/Topic? 0
  • +

Replies To: VB Timer

#2 biggles2008  Icon User is offline

  • Bassface
  • member icon

Reputation: 9
  • View blog
  • Posts: 623
  • Joined: 05-March 08

Posted 20 January 2009 - 02:18 PM

Okay great tutorial, i am currently aiming to improve my knowledge of If...Statements. This tutorial has helped.

Although i must point out. In the last Code the:

Private Sub tmr_Timer()
	 If lblremain.Caption = 0 Then
		  tmr.Enabled = False
		  MsgBox Text1.Text & " sec are up!", vbInformation
	 Else
		  lblremain.Caption = lblremain.Caption - 1
	 End If
End Sub



The
Text1.Text
should be
txtuser.Text
.

But thanks alot :)
Was This Post Helpful? 0
  • +
  • -

#3 freakyjerk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-August 09

Posted 27 August 2009 - 06:54 PM

Thanks a lot!!! this tutorial helped me alot in operating my school project (Filing Cabinet)...

:) :) :) :) :)
Was This Post Helpful? 0
  • +
  • -

#4 joker100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-July 09

Posted 13 September 2009 - 01:39 PM

thank you :) You gave me a idea how I can make a music player alarm with mp3 and wma sound :)
Was This Post Helpful? 0
  • +
  • -

#5 hacksys  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 27-December 08

Posted 23 September 2009 - 08:11 AM

Is there anyway we can create our own timer control with nanosecond support for executing lengthy (i mean extremely lengthy) processes very very quickly.

And im unable to use the following type of coding

Dim i as int32 = 0
for i = 0 to 1
		<code>
next i


This post has been edited by hacksys: 23 September 2009 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

#6 Ruru123  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 09-October 11

Posted 16 October 2011 - 11:13 AM

View Posthacksys, on 23 September 2009 - 08:11 AM, said:

Is there anyway we can create our own timer control with nanosecond support for executing lengthy (i mean extremely lengthy) processes very very quickly.

And im unable to use the following type of coding

Dim i as int32 = 0
for i = 0 to 1
		<code>
next i


hi
can you please just explain this code for me...i'm not quite sure why you have to add this in (but i think it is important- probably why my program works)



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
tmrTimer.Enabled = True
btnStart.Enabled = False
End Sub

Private Sub tmrTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
Counter = Counter - 1
txtCount.Text = Counter

If Counter = 0 Then
tmrTimer.Enabled = False
Counter = 100
End If
End Sub


Thanks

hi

can you please just explain this code for me...i'm not quite sure why you have to add this in (but i think it is important- probably why my program works)

Private Sub tmrTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTimer.Tick
        Counter = Counter - 1
        txtCount.Text = Counter

        If Counter = 0 Then
            tmrTimer.Enabled = False
            Counter = 100
        End If
    End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        tmrTimer.Enabled = True
        btnStart.Enabled = False
    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1