Click here to Skip to main content
11,478,619 members (47,619 online)
Click here to Skip to main content

Multipurpose Digital Clock Control using .NET

, 20 Feb 2004 216.5K 9K 101
Rate this:
Please Sign up or sign in to vote.
A digital clock user control which can be used as a regular clock, stop watch or count down timer. Options include changing display color, freezing the clock and setting multiple alarms.

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.

// set the clock type to DigitalClock
digitalClockCtrl.SetClockType = SriClocks.ClockType.DigitalClock;
// setting display to 24 hour format
digitalClockCtrl.ClockDisplayFormat = SriClocks.ClockFormat.TwelveHourFormat;

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.

// declare a callback method which implements the countdown 
// delegate declared in DigitalClockCtrl and add it to CountDownDone events
private void OnCountDownDone()
{
}
.....
.....
// Add the delegate method to CountDownDone events
// Whenever a countdown is over the clock control calls this delegate
digitalClockCtrl.CountDownDone += new DigitalClockCtrl.CountDown(OnCountDownDone);
......
// set the countdown time
digitalClockCtrl.CountDownTime = ms; // ms - milliseconds
digitalClockCtrl.SetClockType = SriClocks.ClockType.CountDown;

To start the stop watch, set the clock type to ClockType.StopWatch.

// Start the stop watch
digitalClockCtrl.SetClockType = SriClocks.ClockType.StopWatch;

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 freeze the clock display
digitalClockCtrl.SetClockType = SriClocks.ClockType.Freeze;

To change the display color, set the SetDigitalColor property of the control to one of the DigitalColor enumerations.

// setting the digits color to GreenColor
digitalClockCtrl1.SetDigitalColor = SriClocks.DigitalColor.GreenColor;

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 delegates
public void 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.

A list of licenses authors might use can be found here

Share

About the Author

Sriram Chitturi
Architect
United States United States
No Biography provided

Comments and Discussions

 
GeneralLove it!!! Pin
raananv13-Mar-12 1:59
memberraananv13-Mar-12 1:59 
QuestionStopWatch Pin
Mr Apprentice13-Mar-12 0:11
memberMr Apprentice13-Mar-12 0:11 
QuestionAwesome job Pin
Mister Horse11-Nov-11 12:22
memberMister Horse11-Nov-11 12:22 
QuestionGreat Job Pin
carterlist1-Oct-11 2:09
membercarterlist1-Oct-11 2:09 
QuestionHow to use this in a Form Created Using Expression Blend Pin
ewics13-Aug-11 2:34
memberewics13-Aug-11 2:34 
GeneralGeneric GDI+ error Pin
securigy22-Aug-10 5:59
membersecurigy22-Aug-10 5:59 
GeneralRe: Generic GDI+ error Pin
Member 461314621-Aug-11 19:29
memberMember 461314621-Aug-11 19:29 
GeneralMore colors for digits? [modified] Pin
The Mighty Atom27-May-10 7:47
memberThe Mighty Atom27-May-10 7:47 
QuestionMany Instants Not Working.... Pin
ASysSolvers10-Oct-09 8:41
memberASysSolvers10-Oct-09 8:41 
Questionwhy use 111 for A char ? Pin
firas_maher2-Sep-09 20:47
memberfiras_maher2-Sep-09 20:47 
QuestionCompact Framework? Pin
Pengie4-Dec-08 15:22
memberPengie4-Dec-08 15:22 
QuestionAlarm Set help Pin
jackchow13-Nov-08 15:19
memberjackchow13-Nov-08 15:19 
Questionvb 2005? Pin
garyjohn_20006-Sep-07 12:56
membergaryjohn_20006-Sep-07 12:56 
QuestionStart time of Stopwatch Pin
fightclub77717-Jul-07 20:56
memberfightclub77717-Jul-07 20:56 
I'm going to try to implement this tomorrow in my .net web application, but I was wondering if it's possible to set the start time on the stop watch. For example, I need to display an elapsed time when a user loads a page and continue the timer from there. So when the user opens the app, the stopwatch will display 12:05:30 and continue counting up until stopped.

If this is possible, it will be a lifesaver and the only solution I've found for this purpose. Thank you in advance for your help and for this very useful code.

Also, will this work with 2.0?

And one more thing. Is it possible to read the current (frozen) time? This is not a complete necessity as I can figure out the elapsed time based on when it was started and frozen, but for the sake of accuracy, it'd be nice.
QuestionLicense? Pin
KcodeProj5-Jul-07 6:54
memberKcodeProj5-Jul-07 6:54 
AnswerRe: License? Pin
Sriram Chitturi5-Jul-07 7:02
memberSriram Chitturi5-Jul-07 7:02 
GeneralRe: License? Pin
KcodeProj5-Jul-07 23:45
memberKcodeProj5-Jul-07 23:45 
GeneralClock Control Freeze Vales Pin
Mark Gales30-Jun-07 13:38
memberMark Gales30-Jun-07 13:38 
GeneralCountdown Timer Pin
Brizee17-May-07 0:05
memberBrizee17-May-07 0:05 
GeneralRe: Countdown Timer Pin
Sriram Chitturi17-May-07 10:52
memberSriram Chitturi17-May-07 10:52 
QuestionClock for VB.NET - aspx? Pin
Tmuld15-Mar-07 9:13
memberTmuld15-Mar-07 9:13 
AnswerRe: Clock for VB.NET - aspx? Pin
Sriram Chitturi17-May-07 10:55
memberSriram Chitturi17-May-07 10:55 
QuestionNewbie Trouble importing DigitalClock Control to VS 2005 Pin
stabarinde17-Jan-07 12:09
memberstabarinde17-Jan-07 12:09 
Questionmultiple clocks Pin
lnae26-Sep-06 3:14
memberlnae26-Sep-06 3:14 
AnswerRe: multiple clocks Pin
skst31-Oct-06 6:44
memberskst31-Oct-06 6:44 
GeneralRe: multiple clocks Pin
ChristopherZ12-Jun-10 18:46
memberChristopherZ12-Jun-10 18:46 
GeneralClock disabled Pin
jackieflan12-May-06 3:07
memberjackieflan12-May-06 3:07 
GeneralSetting the time Pin
bighoss18-Apr-06 13:12
memberbighoss18-Apr-06 13:12 
QuestionIf i want to get the time shown on the your Control , what should i do? Pin
Your details have been updated6-Sep-05 19:34
memberYour details have been updated6-Sep-05 19:34 
GeneralGMT Pin
Lee Becham8-Jul-05 10:54
memberLee Becham8-Jul-05 10:54 
GeneralUsing two clocks at the same time Pin
carlotino3-Jun-05 13:50
membercarlotino3-Jun-05 13:50 
GeneralRe: Using two clocks at the same time Pin
lnae14-Oct-06 22:38
memberlnae14-Oct-06 22:38 
GeneralProblem with Digital.bmp Pin
carlotino13-May-05 12:07
membercarlotino13-May-05 12:07 
GeneralRe: Problem with Digital.bmp Pin
Sriram Chitturi14-May-05 3:19
memberSriram Chitturi14-May-05 3:19 
GeneralRe: Problem with Digital.bmp Pin
VinceHolmes28-Nov-05 6:49
memberVinceHolmes28-Nov-05 6:49 
GeneralRe: Problem with Digital.bmp Pin
pinturic6-Dec-05 0:17
memberpinturic6-Dec-05 0:17 
GeneralRe: Problem with Digital.bmp Pin
pinturic6-Dec-05 0:27
memberpinturic6-Dec-05 0:27 
JokeRe: Problem with Digital.bmp Pin
LillebrorDE23-Jan-06 2:33
memberLillebrorDE23-Jan-06 2:33 
GeneralRe: Problem with Digital.bmp Pin
Juba27-Dec-06 5:23
memberJuba27-Dec-06 5:23 
GeneralPartially strange behavior Pin
loizzi22-Apr-05 6:13
memberloizzi22-Apr-05 6:13 
QuestionCountDownDone...How to set Clocktime to 00:00:00? Pin
anarcho30-Nov-04 12:50
memberanarcho30-Nov-04 12:50 
GeneralSaved me some time Pin
Tony D. Abel4-Nov-04 10:51
memberTony D. Abel4-Nov-04 10:51 
GeneralSolid Functional Building Block Pin
FlashMerlot22-Oct-04 5:05
memberFlashMerlot22-Oct-04 5:05 
GeneralNot revolutionary... Pin
Robert Rohde16-Feb-04 0:09
memberRobert Rohde16-Feb-04 0:09 
GeneralRe: Not revolutionary... Pin
Sriram Chitturi16-Feb-04 3:54
memberSriram Chitturi16-Feb-04 3:54 

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
Web01 | 2.8.150520.1 | Last Updated 21 Feb 2004
Article Copyright 2004 by Sriram Chitturi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid