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

Welcome to Dream.In.Code
Become an Expert!

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




How to use timers to make a flashing digital clock

 
Reply to this topicStart new topic

> How to use timers to make a flashing digital clock, This tutorial mainly shows you how to use timers on a windows form app

Rating  5
VB.Terry
Group Icon



post 10 Oct, 2009 - 06:26 AM
Post #1


Don't copy and paste to learn anything type it out!!!!

Hello today I will be showing you how to make a flashing digital clock. To create one follow these directions.
1) Create a new windows form application. (Name what ever you want)
A windows form application will build a program interace where you can add contols to.

2) Add a label to the form. If you can't find the label click on the toolbox that is on the left side of visual basic.(The label will show the date and time so resize your form close to what you think it would be with that text.)

3) I recommend you rename the label lblDateAndTime so that if someone else was looking at your project (or you) would understand what that label is exposed to do.

4) Add two timers to the form. To get a timer goto the toolbox and scroll down until you find it double click on it. Goto the properties of the timer/s and set the interval to any number, this will update the text by milliseconds so lets say you put in 70 milliseconds the text will change colors every 70 milliseconds.(or drag it to the form. One would suffice but I use two to ensure the program don't slow down.)
5) Here is where we start adding code:

6) For form1's load event add this code:
CODE
Timer1.Start()
        Timer2.Start()
(Or if you have one timer.)
CODE
Timer1.Start()


The reason we use
CODE
Timer1.Start()
is so that when the user opens the application the program will start doing what ever you put into the timers tick event.

7) Add this code to timer1 tick event
CODE
lblDateAndTime.Text = DateAndTime.Now
. We want our label to say the current date but we also want the label to keep updating its text thats why for the form 1 load event we have this code
CODE
Timer1.Start()
this will interact with timer 1's tick event so that the text of the label will keep updating. (If you have one timer.)
CODE
lblDateAndTime.Text = DateAndTime.Now
If lblDateAndTime.ForeColor = Color.White Then
            lblDateAndTime.ForeColor = Color.Red
        ElseIf lblDateAndTime.ForeColor = Color.Red Then
            lblDateAndTime.ForeColor = Color.Yellow
        ElseIf lblDateAndTime.ForeColor = Color.Yellow Then
            lblDateAndTime.ForeColor = Color.Green
        ElseIf lblDateAndTime.ForeColor = Color.Green Then
            lblDateAndTime.ForeColor = Color.Pink
        ElseIf lblDateAndTime.ForeColor = Color.Pink Then
            lblDateAndTime.ForeColor = Color.Blue
        ElseIf lblDateAndTime.ForeColor = Color.Blue Then
            lblDateAndTime.ForeColor = Color.Black
        ElseIf lblDateAndTime.ForeColor = Color.Black Then
            lblDateAndTime.ForeColor = Color.White
        End If

8) Timer2 tick event
CODE
If lblDateAndTime.ForeColor = Color.White Then
            lblDateAndTime.ForeColor = Color.Red
        ElseIf lblDateAndTime.ForeColor = Color.Red Then
            lblDateAndTime.ForeColor = Color.Yellow
        ElseIf lblDateAndTime.ForeColor = Color.Yellow Then
            lblDateAndTime.ForeColor = Color.Green
        ElseIf lblDateAndTime.ForeColor = Color.Green Then
            lblDateAndTime.ForeColor = Color.Pink
        ElseIf lblDateAndTime.ForeColor = Color.Pink Then
            lblDateAndTime.ForeColor = Color.Blue
        ElseIf lblDateAndTime.ForeColor = Color.Blue Then
            lblDateAndTime.ForeColor = Color.Black
        ElseIf lblDateAndTime.ForeColor = Color.Black Then
            lblDateAndTime.ForeColor = Color.White
        End If


When ever you want text or a form to flash you need to start the code with the current color of the form or text, in the middle of the code there would be all the colors you want the text to be, the last piece of that code will be the same color as the form or text. This will make the text keep changing colors even when it reaches the end of the code.

9) Here is the final code:
CODE
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
        Timer2.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        lblDateAndTime.Text = DateAndTime.Now
    End Sub

    Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If lblDateAndTime.ForeColor = Color.White Then
            lblDateAndTime.ForeColor = Color.Red
        ElseIf lblDateAndTime.ForeColor = Color.Red Then
            lblDateAndTime.ForeColor = Color.Yellow
        ElseIf lblDateAndTime.ForeColor = Color.Yellow Then
            lblDateAndTime.ForeColor = Color.Green
        ElseIf lblDateAndTime.ForeColor = Color.Green Then
            lblDateAndTime.ForeColor = Color.Pink
        ElseIf lblDateAndTime.ForeColor = Color.Pink Then
            lblDateAndTime.ForeColor = Color.Blue
        ElseIf lblDateAndTime.ForeColor = Color.Blue Then
            lblDateAndTime.ForeColor = Color.Black
        ElseIf lblDateAndTime.ForeColor = Color.Black Then
            lblDateAndTime.ForeColor = Color.White
        End If
    End Sub
End Class



Hope you my tutorial it! biggrin.gif
Goto Env X to post questions you may have about programming or if you want to post a tutorial!

This post has been edited by VB.Terry: 10 Oct, 2009 - 11:27 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Martyr2
Group Icon



post 10 Oct, 2009 - 08:43 AM
Post #2
In a tutorial you typically want to tell everyone what is going on at each step rather than just showing code. Someone who would read this tutorial may be a newbie and don't know what the code is doing or why. Tutorials teach and is more involved than merely showing code and where to put it. Keep this in mind and you may want to either update this tutorial or write better explanations in future tutorials.

Thanks for the contribution.
Go to the top of the page
+Quote Post

VB.Terry
Group Icon



post 10 Oct, 2009 - 11:28 AM
Post #3
QUOTE(Martyr2 @ 10 Oct, 2009 - 08:43 AM) *

In a tutorial you typically want to tell everyone what is going on at each step rather than just showing code. Someone who would read this tutorial may be a newbie and don't know what the code is doing or why. Tutorials teach and is more involved than merely showing code and where to put it. Keep this in mind and you may want to either update this tutorial or write better explanations in future tutorials.

Thanks for the contribution.

Thank you for the advice! biggrin.gif
Go to the top of the page
+Quote Post


Fast ReplyReply 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/21/09 02:23PM

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