Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
How To Create An Analog Clock in Visual Basic.Net (VB.Net)

Author: DevASP
Download Source Code : 302_AnanlogClock.zip

This article is about how you can create an analog clock using the system timer control in VB.NET. This is a real analog clock with full functionality.

Follow the following steps to accomplish task:

 

  • Start visual studio and create a new window application for VB.NET
  • Drop a picture box control from the toolbox to your form.
  • Declare a timer control with the following statement in your code window.

Friend WithEvents Timer1 As System.Timers.Timer

 

  • Set the following properties for picture box control

Name = Clock

Width = 376

Height = 376

BackgroundImage = “image that you want to display in the background of your clock”

 

 

  • Set the following properties of the system.timer control

            Enabled = True

Interval = 1000

SynchronizingObject = Me ‘here me represents the Form

 

  • Set the following properties for the Form

            Name = Form1

Text = Analog Clock

 

  • Now declare the variables of type bitmap as follow:

 Public MinuteP As Bitmap = New Bitmap(Application.StartupPath + "\minute.bmp")

 

 Public SecondP As Bitmap = New Bitmap(Application.StartupPath + "\second.bmp")

 

 Public HourP As Bitmap = New Bitmap(Application.StartupPath + "\hour.bmp")

 

 Public CenterP As Bitmap = New Bitmap(Application.StartupPath + "\center.bmp")

 

 Public TMPIMG As Bitmap = New Bitmap(376, 376)

 

 

Public Graph As Graphics = Graphics.FromImage(TMPIMG)

 

  • Write the following code in the load event of your Form:

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

       

     Timer1_Elapsed(Nothing, Nothing)

 

End Sub

 

  • Write the following code in the Elapsed event of the timer control:

Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As

System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed

Graph.DrawImage(ClockI.BackgroundImage, 0, 0, 376, 376)

 

Graph.TranslateTransform(188, 188)

Graph.RotateTransform(-90 + ((Now.Hour * 30) + (Now.Minute * 0.5)))

 

Graph.DrawImage(HourP, -13, -13)

Graph.ResetTransform()

 

 

Graph.TranslateTransform(188, 188)

Graph.RotateTransform(-90 + (((Now.Minute * 60) + Now.Second) * 0.1))

 

Graph.DrawImage(MinuteP, -13, -13)

Graph.ResetTransform()

 

Graph.TranslateTransform(188, 188)

Graph.RotateTransform(-90 + (Now.Second * 6))

Graph.DrawImage(SecondP, -13, -13)

Graph.ResetTransform()

 

Graph.TranslateTransform(188, 188)

Graph.DrawImage(CenterP, -18, -18)

Graph.ResetTransform()

ClockI.Image = TMPIMG

 

End Sub

 

Before going to execute the project be sure that you have the images in bin folder of your application. These images are used to display hour, minute and second needles. These images are just colorful lines in the bitmap format.

 

You can change then name of files but be sure that you have given the correct names in the declaration of bitmap images above in the code.

 

Here in this application the names are.

  • Minute
  • Second
  • Hour
  • Center

Center is used to combine all these at a single point in the clock center. Thats why they seem to be met each other.

Article Comments
The ZIP file is invalid

Posted on 3/24/2008 8:55:56 PM by Joe Sweeney

good post.. helped me... thanks..

Posted on 3/10/2009 5:58:00 AM by priya

Thanks the code was good

Posted on 11/12/2009 3:54:39 PM by pirate

thanks a lot...
nice code.simple and smart

Posted on 1/29/2010 9:16:10 PM by Kabir Ali

I tried to change the hands with my graphics, but now when the hands move, the center point of each hand seems to shift a little. Is the shape and size of the hands? Also please explain what the following code does:
Graph.TranslateTransform(188, 188)
Graph.RotateTransform(-90 + ((Now.Hour * 30) + (Now.Minute * 0.5)))

Graph.DrawImage(HourP, -13, -13)
Graph.ResetTransform()

Posted on 2/21/2010 8:29:52 PM by Syed Gilani

I want dll file of clock which is useful me to add in vb.net Application

Posted on 4/29/2010 7:14:42 AM by Mayur Patil

Thanks:-)

Posted on 8/30/2010 8:45:19 PM by Ken Arce

Thanks

Posted on 11/2/2010 2:31:15 PM by Arun

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to sort ListViewItems in Visual Studio 2005

Disclaimer - Privacy
© 2002-2014 DevASP.net