School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,288 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,073 people online right now. Registration is fast and FREE... Join Now!




VB Timer

 
Reply to this topicStart new topic

> VB Timer, This tutorial shows you how to code a simple timer.

benpap
Group Icon



post 6 Dec, 2007 - 11:15 AM
Post #1


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.
  1. a label as a title (lbltitle)
  2. a text box (txtuser) where the user enters a the time he wants to countdown in seconds
  3. another label (lblremain) where the remaining time is displayed
  4. a timer (tmr) which is the "heart" of the application
  5. 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)

CODE

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):
CODE

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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

biggles2008
Group Icon



post 20 Jan, 2009 - 01:18 PM
Post #2
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:

CODE
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
CODE
Text1.Text
should be
CODE
txtuser.Text
.

But thanks alot smile.gif
Go to the top of the page
+Quote Post

freakyjerk
*



post 27 Aug, 2009 - 05:54 PM
Post #3
Thanks a lot!!! this tutorial helped me alot in operating my school project (Filing Cabinet)...

smile.gif smile.gif smile.gif smile.gif smile.gif
Go to the top of the page
+Quote Post

joker100
*



post 13 Sep, 2009 - 12:39 PM
Post #4
thank you smile.gif You gave me a idea how I can make a music player alarm with mp3 and wma sound smile.gif
Go to the top of the page
+Quote Post

hacksys
Group Icon



post 23 Sep, 2009 - 07:11 AM
Post #5
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

CODE

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


This post has been edited by hacksys: 23 Sep, 2009 - 07:11 AM
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 12:16PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month