Page 1 of 1

How to use timers to make a flashing digital clock This tutorial mainly shows you how to use timers on a windows form app Rate Topic: ***** 1 Votes

#1 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Post icon  Posted 10 October 2009 - 07:26 AM

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:
Timer1.Start()
		Timer2.Start()
(Or if you have one timer.)
Timer1.Start()


The reason we use
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
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
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.)
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
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:
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! :D
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 October 2009 - 12:27 PM


Is This A Good Question/Topic? 3
  • +

Replies To: How to use timers to make a flashing digital clock

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Posted 10 October 2009 - 09: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.
Was This Post Helpful? 0
  • +
  • -

#3 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Posted 10 October 2009 - 12:28 PM

View PostMartyr2, on 10 Oct, 2009 - 08:43 AM, said:

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! :D
Was This Post Helpful? 0
  • +
  • -

#4 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Posted 12 February 2010 - 08:53 AM

Does anyone even like this tutorial.
Was This Post Helpful? 0
  • +
  • -

#5 Stippadippa  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 10-July 09

Posted 24 February 2010 - 08:37 AM

Thanks for this tutorial, I was stuck on this problem for while and i finally solved it. Flashing things was problem i was figuring out. Thanks a lot again.

This post has been edited by Stippadippa: 24 February 2010 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#6 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 357
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Posted 24 February 2010 - 02:32 PM

View PostStippadippa, on 24 February 2010 - 07:37 AM, said:

Thanks for this tutorial, I was stuck on this problem for while and i finally solved it. Flashing things was problem i was figuring out. Thanks a lot again.

Your welcome.
Was This Post Helpful? 0
  • +
  • -

#7 digitaldevelopment  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 168
  • Joined: 30-April 12

Posted 29 May 2012 - 09:06 AM

thanks, this was really helpfull ^^ now I finally know how you use timers :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1