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!
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






MultiQuote





|