Introduction

The Digital Clock Control is a versatile user control with options for:

Digital clock

Stop watch

Count down timer

Setting multiple alarms

Freezing the clock

Multiple color display

Resizable display

The digits can be displayed in three different colors - Red, Blue and Green.

The control adjusts the digits size with the size of the rectangle of the control. A ratio of 1:2 is maintained between the height and the width of the large digits and half the size for smaller ones. The clock control also supports 2 types of formats - 12 hour and 24 hour.

Using the Digital Clock control

Place the SriClocks.dll and Digital.bmp files in a single directory. At design time, right click in the ToolBox of Visual Studio, browse for and select SriClocks.dll file. The following image shows the control selected:

On pressing OK, the control is added to the ToolBox as shown below:

Now, drag and drop the control on to a form to create the Digital Clock control as any other user control.

Demo project - TestClock.exe

The demo project included shows all the capabilities and usage of the Clock control. Here is a screenshot.

Code to use the DigitalClockCtrl

To display a normal watch, which is also the default, just set the clock type to DigitalClock as shown below. Optionally, you may set the 24 hour or 12 hour format using ClockDisplayFormat of DigitalClockCtrl. The 12 hour format will display 'A' for AM and 'P' for PM. The default is the 12 hour format.

To use the countdown timer, create and add delegates to the clock control's CountDownDone event. Then set the clock type to CountDown. When the countdown is over, the clock control will fire the CountDownDone event.

To freeze the clock display, set the clock type to ClockType.Freeze. This will freeze the clock till the clock type is reset to DigitalClock, CountDown or StopWatch. The functionality is particularly useful when using the stopwatch.

To set alarms, create delegates and add them to RaiseAlarm event of the clock control. When an alarm is raised by the control, it fires the RaiseAlarm events.

// Create the Alarm delegatespublicvoid OnRaiseAlarm()
{
}
....
....
// add the delegate to the RaiseAlarm event
digitalClockCtrl.RaiseAlarm += new DigitalClockCtrl.Alarm(OnRaiseAlarm);
....
// set the alarm which accepts values as System.DateTime structure.// DateTime.Parse is used here to parse the input text from user// When the alarm is raised by the control// the OnRaiseAlarm method declared above is called
digitalClockCtrl.AlarmTime = DateTime.Parse(Alarmtime.Text);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Comments and Discussions

The clock is great. (I'm using it on .Net 2005)
But when i try to display 2 clocks on the same form it doesn't work. any idea why?! (to "work around" this problem i created another dll and now i have 2 refrences with different names)

Hi there. I'm not sure if its something I'm doing incorrectly but the clock doesn't seem to work correctly on my web project. If I create and add it to a windows application it works correctly, but as soon as I try to add it to a project that is located on a server, not on my machine, the control becomes disabled. Is there any way that I can get around this?

I tried putting two instances of the digital clock control in the same form, but they both seem to have problems rendering the digits. The same happens if I use two separate forms, with a component each.

I encountered a problem while trying to use the Digital Clock Control. At first it was installed successfully, but after closing Visual Studio .net 2003 and opening it again, when I tried to open the form where I used the component I got the following error message: "An error occured while loading the document. Fix the error, and then try loadoing the document again. The error message follows: Digital.bmp"
Also when I try to add the Digital Clock to a form I get the same message (Digital.bmp).

Fantastic control, by the way. Very simple in what it does, and very easy to work with. The way controls should be!

I too have the Digital.bmp problem and i can't get rid of it. I now have the dll and the bmp files in my project source directory as well as the output folders. Everything works fine in the IDE, but as soon as i exit VS and then open it up again i get the error message.

I tried logging with filemon (www.sysinternals.com) all the accesses to file:
amd i found out that for some strange reason that file is searched in the Microsoft Visual Studio IDE folder (in my case D:\Programs\Microsoft Visual Studio .NET 2003\Common7\IDE).

When putting the digital.bmp file there the error disappear (even if not really professional! ).

Same thing here, with another strange subtype:
In my case the File "Digital.bmp" was searched in the project's parent folder, e.g. when the project was located at "C:\Projects\Digitalclock" the file had to be located at "C:\Projects\Digital.bmp".

Easy... just go to the main project and click the Clock.bmp and set the property "Build Action" to be "Embedded Resource" instead of "Content" as it comes by default - do the same for the icon file. Now reload your SriClocks.dll on your project (maybe it won't be needed). Hope it helps!

"Happy is the man that finds wisdom, and the man that gains understanding."
Proverbs 3:13

I want use this control in a shareware program, I think there is no problem to do so. But a few things bother me at this time.

If I set the color of the control to other than red, the display shows some drawing errors. Parts of prior numbers stay in display. If I set the color back to red, all is fine. If I place in Visual Studio 2003 the control on a form, the control is available in the control list in the properties box. Next time I start VS2003 the control disapears from the list but still remains in the program. There is no problem to compile the programm. The control works in the running program, but there is no way to grab its properties due properties box.

Often, people try too hard. They add dozens of bells-n-whistles which 95% of applications do not need. This is an EXCELLENT example of "Just-Da-Basics" where (since the user has source code) the user can add just those bells-n-whistles needed for their project.

...but very nice.
Just a few notes:
1. The link which should contain the demo project doesn't work. In the other link a demo project is included.
2. Why only 3 Colors? Make it really user definable.
3. A property to switch off the 10th seconds would also be nice.

All the rest looks very good. If I need a clock in one of my next projects I'll probably use this one.

Thanks Robert. Fixed the demo link.
There are infinite number of combinations possible for improvement.
As the source code is included, I leave it to an enthusiast to develop on it.
If you want a customized control please contact me offline at my mail address.
Have a nice day.