Click here to Skip to main content
Click here to Skip to main content

Analog Clock Control

, 23 Jan 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
The analog clock control is a control that has almost all the functionality that a clock control can have, and it is fully modifiable.


Here, an analog clock has been created with a VB.NET control library. It is a clock control which has almost all the functionality that this type of control can have, and it is fully customizable. Since this is a control library, you can use it in C++, C#, J#, and VB.NET projects in the .NET environment.

I created this control to help someone in VB Forums. At the beginning, it was a very simple clock, but then it became somewhat advanced after I added many properties, events, and a functionality that makes the clock very flexible.


Adding the Component

To use the control, you just need to add it into the VS.NET Toolbox. Right click in the Toolbox area, and select the "Choose Items" menu item. It will then open the "Choose Toolbox Item" window. You go to the directory that contains the "AnalogClockLib.dll" file and select it, then click the OK button. This will add the control onto the Toolbox.

Finally, you drag and drop the control onto your forms. Also, in order to see the descriptions of the properties or methods in the Code Designer, you should copy the "AnalogClockLib.xml" file into your project's folder.


The clock control is a Windows UserControl. Almost all the elements of the clock have been constructed (the core of the element) with the GraphicsPath data type. They contain a member variable Base-Path which is a GraphicsPath of the element. These Base-Paths are used differently for each element. For example, the marker's Base-Path represents a GraphicsPath constructed at 12 hour position and than rotated using a Matrix object. Since it is rotated only once, there is no need fore any other helper objects. The clock's hands have two member variables of type GraphicsPath: Base-Path and Shift-Path. The Base-Path of the hands are always positioned at 12 o' clock, and only reshaped if the element's shape (width, length, or style) is modified. On the other hand, the Shift-Path is the actual GrapicsPath of the hand at any given time. Shift-Path is the copy of the rotated Base-Path.

Using the Code

Although you can do almost anything with this control, I will show you only how to paint the hour-hand of the clock with a PathGradientBrush. Note, in this fashion, you can paint the elements with any brush.

It is very nice to see the clock hand's gradient, so here is how you can do that. Basically, you set the Brush property of the hands to a newly created gradient brush object in the hands' paint event.

Private Sub Clock1_HourHandPainting(ByVal sender As Object, _
        ByVal e As AnalogClock.PaintEventArgs) Handles Clock1.HourHandPainting
    'Make sure the hour hand's graphics path contains more than 2 points.
    If Me.Clock1.HourHand.Path.PointCount > 2 Then
        'Make the hour hand gradient
        Dim br As New Drawing2D.PathGradientBrush(Me.Clock1.HourHand.Path)
        br.CenterColor = Color.White
        br.SurroundColors = New Color() {Me.Clock1.HourHand.Color}
        e.Brush = br
    End If
End Sub

In some situations, you may need to set the clock time to a start time. For this, we need to do a little bit of calculation and set the UtcOffset property accordingly. Here is how we can do that:

Private Sub Form1_Load(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Load
    Dim cdt As DateTime = CDate("#17:20:35#")  'Some custom start time for the clock.
    Dim utcDt As DateTime = DateTime.UtcNow  'The current UTC dateTime. 
	'This is needed because the clock internal works with UTC dateTime.
    Me.Clock1.UtcOffset = New TimeSpan(0, cdt.Hour - utcDt.Hour, _
                             cdt.Minute - utcDt.Minute, cdt.Second - utcDt.Second)
End Sub

Sometimes people ask me why the clock is one hour off after daylight savings or how we can be sure that clock shows the correct time always. Well, if this is the case with you, then you need to make sure that the clock's UtcOffset is always accurate. This is how you can do it:

Private Sub Form1_Load(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles MyBase.Load
    'Set UTC offset to the system utc offset when the application loads
    Me.Clock1.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)
End Sub

Private Sub Clock1_TimeChanged(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles Clock1.TimeChanged
    'Set UTC offset to the system utc offset every time clock time changes. 
    'If the property has the same value it will do nothing.
    Me.Clock1.UtcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now)
End Sub

For more examples, check the demo project.

Analog Clock Control Information


  • -- Original version posted
  • 1.0.2813.38699 -- Some minor changes to the number and marker styles. Also. the UTC offset values range is changed that accepts values from -23:00:00 to 23:00:00.
  • 1.0.2847.27310 -- This update adds a property that can be used to get/set the clock's frame line color. Also, there are some corrections to the clock. The update is recommended.
  • 1.0.2917.23783 -- A small bug fix
  • 1.0.3002.36694 -- Added minute marker width property
  • 1.0.3223.6598 -- This update includes major changes to the implementation of the clock. If you liked the previous versions of the analog clock, then you will love this one. In this version, you can modify every element of the clock at design and run time. There is a lot of good stuff I would like to mention, but it is better you try it yourself. I will remind you to check all the properties of the clock, in particular, the Element category in the Properties window which contains the clock's hands, markers, etc. I hope you will like it.
  • -- Some bug fixes


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) ZipEdTech
United States United States
No Biography provided

Comments and Discussions

QuestionCannot run sample code Pinmemberhobbitdome15-Jan-14 20:17 
QuestionText at the top and digital clock at the bottom PinmemberAnthony Geselle2-Apr-13 5:25 
Hi! I can't seem to find the attribute to change or add the small text at the top of the clock-control (or enable the small digital clock at the bottom of the clock-face)...
What am I missing?
Otherwise I give this 5 out of 5 !
QuestionAbout Analog Clock Control PinmemberRajiv nayan3-Dec-12 0:07 
QuestionHow to get it work in .net framework 4.0 Pinmemberzafar.shakeel5-Nov-12 21:53 
QuestionIs there a way I could have one of the hand follow a different input than time based PinmemberDavid Caracciolo24-Oct-12 10:26 
GeneralMy vote of 5 Pinmembermanoj kumar choubey23-Feb-12 22:53 
GeneralMy vote of 5 PinmemberLogeco19-Jun-11 20:05 
GeneralSuggestions PinmemberWomblingFree6-Jun-11 14:50 
Generala 4.0 Framework version? Pinmemberhedgefundit2-Jun-11 20:04 
QuestionControl crippling forms designer? PinmemberClaude St20-Feb-11 14:22 
AnswerRe: Control crippling forms designer? PinmemberVBDT20-Feb-11 19:55 
GeneralRe: Control crippling forms designer? Pinmembersunco23-Dec-12 15:42 
QuestionIs there any way to fit it to .net 4 client profile? Pinmemberyanivabo26-Dec-10 7:23 
GeneralMy vote of 5 Pinmembermalcomm8-Dec-10 16:21 
GeneralAnalogClockControl for framework 4.0 Pinmemberamit sonkhiya18-Oct-10 22:31 
GeneralRe: AnalogClockControl for framework 4.0 Pinmemberkathan_shah329-Feb-12 18:54 
SuggestionRe: AnalogClockControl for framework 4.0 Pinmemberamit sonkhiya9-Feb-12 21:43 
GeneralMy vote of 5 Pinmemberbjondo7-Oct-10 21:28 
Generalcool! Pinmemberchiao.vincent26-Jan-10 16:14 
Generalnice article PinmembercrudeCodeYogi24-Jan-10 7:28 
GeneralGot an error in my VB 2008 Express Project Pinmemberrace66611-Jan-10 5:04 
GeneralRe: Got an error in my VB 2008 Express Project PinmemberVBDT11-Jan-10 16:57 
GeneralRe: Got an error in my VB 2008 Express Project Pinmemberrace66611-Jan-10 21:16 
GeneralRe: Got an error in my VB 2008 Express Project PinmemberVBDT12-Jan-10 11:20 
GeneralRe: Got an error in my VB 2008 Express Project Pinmemberrace66612-Jan-10 20:45 
GeneralRe: Got an error in my VB 2008 Express Project PinmemberVBDT12-Jan-10 21:01 
GeneralRe: Got an error in my VB 2008 Express Project Pinmemberrace66612-Jan-10 21:40 
GeneralRe: Got an error in my VB 2008 Express Project PinmemberVBDT13-Jan-10 8:35 
GeneralRe: Got an error in my VB 2008 Express Project Pinmemberrace66613-Jan-10 23:14 
QuestionDaylight Savings Time???? PinmemberFigmo224-Dec-09 13:35 
AnswerRe: Daylight Savings Time???? PinmemberVBDT13-Jan-10 17:33 
GeneralRe: Daylight Savings Time???? PinmemberFigmo214-Jan-10 9:38 
GeneralRe: Daylight Savings Time???? PinmemberVBDT14-Jan-10 16:19 
QuestionThis is working with which version of .net? [modified] Pinmemberunmesha27-Oct-09 3:53 
GeneralIs possible make in c# Pinmemberprof30007-Sep-09 22:27 
GeneralRe: Is possible make in c# PinmemberVBDT8-Sep-09 10:09 
QuestionStops cursor keys working? PinmemberRoger CO16-Jul-09 3:06 
AnswerRe: Stops cursor keys working? PinmemberVBDT6-Aug-09 8:34 
GeneralUnfair PinmemberRunnerDude29-Mar-08 7:51 
GeneralRe: Unfair PinmemberVBDT16-Nov-08 10:42 
GeneralNo code! PinmemberAChaves25-Mar-08 8:53 
GeneralRe: No code! PinmemberVBDT16-Nov-08 10:41 
QuestionSetting the Time? PinmemberJoe Sweeney24-Mar-08 17:39 
AnswerRe: Setting the Time? PinmemberNeoTheKiller13-Oct-08 6:26 
AnswerRe: Setting the Time? [modified] PinmemberVBDT13-Oct-08 13:57 
AnswerRe: Setting the Time? PinmemberVBDT28-Oct-08 1:54 
GeneralRe: Setting the Time? PinmemberMember 22547713-Aug-09 12:54 
GeneralRe: Setting the Time? PinmemberVBDT3-Aug-09 13:35 
GeneralRe: Setting the Time? Pinmembersupercat96-Aug-09 5:52 
GeneralControl running in VS2008 Pinmemberpeterennis12-Mar-08 8:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 23 Jan 2010
Article Copyright 2007 by VBDT
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid