# VBForums UtilityBank > UtilityBank - Utilities >  Analog Clock Control

## VBDT

The Analog Clock Control is created with 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 .Net environment.

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



You can do more exciting stuff with this clock control. My Desktop Alarm Clock that uses the control. All these styles are easy to create. You simply set the clock's background image and set its elements' properties. And yes, you need to fill the elements with gradient brushes.



*Background:*
The _Analog Clock_ control is a Windows _UserControl_. Almost all the elements of the clock (except the _Symbols_) have been constructed (the core of the element) with _GraphicsPath_ object. They contain a member variable Base-Path which is a _GraphicsPath_ of the element. These Base-Paths are used differently for each type of element. For example, each marker Base-Path represents a _GraphicsPath_ object constructed at 12 hour position and than rotated using a _Matrix_ object. Since it is rotated only once, there is no need for 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.

The clock control's elements are painted layer by layer. Here is the order of the elements' paint that also raises the events.
Background PaintSmall-Markers PaintBig-Markers PaintSymbols PaintHour-Hand PaintMinute-Hand PaintSecond-Hand PaintCenter-Point PaintFrame PaintPaint

My objective was to create an analog clock control whose elements could be customizable at design and run time. Although every element supports at leas two styles, a custom style (a _GraphicsPath_ object) can be provided by the developer in the elements' _CustomElementRequest_ event handler. Moreover, the elements can be filled with different brushes at run time in the elements' _ElementPainting_ event handler. All is needed a brush to be assigned to the _AnalogClock.PaintEventArgs_ argument in the event handler. I have tried to make the Analog Clock control as a true control and I think I have succeeded.

*Update History:*

Assembly Version: 1.5.0.0
File Version:         1.0.0.2

Note this version of the control is not compatible to the previous versions. There are many internal changes including some object names.
There was a design time issue that is fixed in this version. After making some changes to the elements properties in the Collection Editor and clicking the Cancel button, the Collection Editor was behaving abnormal and the objects were becoming uncontrollable. This is because after clicking the cancel button the Collection Editor was returning new element objects so the attached event handlers were getting lost.Overall, this version fixes all design time issues and the code is optimized.

Assembly Version: 1.5.0.2
File Version:         1.0.0.4
Some bug fixes and improvements (update is recommended).Added one mere Center-point style (Octagon).
Assembly Version: 1.5.0.3
File Version:         1.0.0.5
A bug fix for the elements' *Tag* property at design time (update is recommended).
_Please feel free to report bugs or comment!_

*Using the code:*

Although, you can do almost anything with this control, I will show you how to paint the Hour, Minute and Second hands of the clock with PathGradientBrush. Note in this fashion you can paint the elements with any brush.

It is very nice to see the clock's hands gradient so here is how you can do it. Basically, you set the brush property of the hands to a newly created gradient brush object in the hands' painting events. For more examples check the demo project.


vb Code:
Private Sub Clock1_HandsPainting(ByVal sender As Object, ByVal e As AnalogClock.PaintEventArgs) _Handles Clock1.SecondHandPainting, Clock1.MinuteHandPainting, Clock1.HourHandPainting     'Get reference to the hand object.    Dim h As Hand = DirectCast(sender, Hand)    'Make sure the hand's graphics path contains more than 2 points.    If h.Path.PointCount > 2 Then        'Make the hand gradient.        Dim br As New Drawing2D.PathGradientBrush(h.Path)        br.CenterColor = Color.White        br.SurroundColors = New Color() {h.Color}        e.Brush = br        br.Dispose()    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 calculation and set the UtcOffset property accordingly. Here how we can do that:

vb Code:
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 beacuse 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
Some time people ask me why the clock is one hour off after daylight savings or how can we be sure that clock shows the correct time always.
Well, if this is the case with you than you need to make sure that clock’s UtcOffset is always accurate. This is how you can do it:

vb Code:
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

Download the latest version *here*

----------


## RobCrombie

It would be nice if it was a VB6 User Control (Nothing has to be registered).

From my quick look, it -
- does not appear to be UC
- requires registering the DLL 
- Only for VB.NET

Ah, such is life!

Rob

----------


## aymaans

Thank You Very Much Realy Good Work

----------


## ktab

Great one, thank you.

May I suggest an update to users, another solution in order not to set manually UTCoffset...

Place next function in your solution:



```
Public Function GetUTCOffset() As TimeSpan
        Dim baseUTC As DateTime = DateTime.Now
        Dim localZone As TimeZone = TimeZone.CurrentTimeZone
        Dim localTime As DateTime = localZone.ToLocalTime(baseUTC)
        Dim localOffset As TimeSpan = localZone.GetUtcOffset(localTime)
        Return localOffset
    End Function
```

at form load event, use this:


```
Clock1.UtcOffset = GetUTCOffset()
```

And another trick i like alot, at double click event of the control:


```
Private Sub Clock1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs) Handles Clock1.DoubleClick
        Shell("rundll32.exe shell32.dll,Control_RunDLL  timedate.cpl,,0")
    End Sub
```

----------


## dgvb

Greetings,

----------


## sunco

This post is old but very good one.. last edited Feb 27th

Did you found a solution for the 4.0 crippling/freeze issue ? I'm using VB 2010 express

----------

