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

Multipurpose Digital Clock Control using .NET

By , 20 Feb 2004
 

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

About the Author

Sriram Chitturi
Architect
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralLove it!!!memberraananv13 Mar '12 - 0:59 
QuestionStopWatchmemberMr Apprentice12 Mar '12 - 23:11 
QuestionAwesome jobmemberMister Horse11 Nov '11 - 11:22 
QuestionGreat Jobmembercarterlist1 Oct '11 - 1:09 
QuestionHow to use this in a Form Created Using Expression Blendmemberewics13 Aug '11 - 1:34 
GeneralGeneric GDI+ errormembersecurigy22 Aug '10 - 4:59 
GeneralRe: Generic GDI+ errormemberMember 461314621 Aug '11 - 18:29 
GeneralMore colors for digits? [modified]memberThe Mighty Atom27 May '10 - 6:47 
The author probably doesn't care any more, but maybe some else is able to extend this a little.
 
I'd like to add some more colors for the digits, like yellow, cyan, purple, orange, hotpink.
Im having a very hard time reading and understanding the code behind this, as im a VB.Net hobby programmer and C# is not my preferable language.
 
Basically, you need to add those colors to the DigitalColor enum. I can do that, but i have no idea what else to modify. I found these:
 
	static private void setDimPen()
	{
		if (dimPen == null) dimPen = (Pen)pen.Clone();
		dimPen.Width = pen.Width;
            	dimPen.Color = Color.FromArgb(
                	pen.Color.R > 0 ? 60 : 0,
                	pen.Color.G > 0 ? 60 : 0,
               		pen.Color.B > 0 ? 60 : 0);
        }
 
	static internal void SetPenColor(DigitalColor dclr)
	{
		pen.Color = Color.FromArgb(
			(dclr == DigitalColor.RedColor) ? 255:0,
               		(dclr == DigitalColor.GreenColor) ? 255:0,
			(dclr == DigitalColor.BlueColor) ? 255:0);
		setDimPen();
	}
 
Im guessing i need to modify those, but im not sure how, it doesn't make sense to me. 255Blush | :O for example, should'nt that be 255Blush | :O Blush | :O because that's the RGB value for red.
 
Who's willing to help me and extend this awesome clock control with some more colors for the digits?
 
EDIT Jan 12 2011:
I was right, he doesn't care. Nobody does. Dead | X|
http://www.themightyatom.nl
modified on Tuesday, January 11, 2011 7:34 PM

QuestionMany Instants Not Working....memberASysSolvers10 Oct '09 - 7:41 
Questionwhy use 111 for A char ?memberfiras_maher2 Sep '09 - 19:47 
QuestionCompact Framework?memberPengie4 Dec '08 - 14:22 
QuestionAlarm Set helpmemberjackchow13 Nov '08 - 14:19 
Questionvb 2005?membergaryjohn_20006 Sep '07 - 11:56 
QuestionStart time of Stopwatchmemberfightclub77717 Jul '07 - 19:56 
QuestionLicense?memberKcodeProj5 Jul '07 - 5:54 
AnswerRe: License?memberSriram Chitturi5 Jul '07 - 6:02 
GeneralRe: License?memberKcodeProj5 Jul '07 - 22:45 
GeneralClock Control Freeze ValesmemberMark Gales30 Jun '07 - 12:38 
GeneralCountdown TimermemberBrizee16 May '07 - 23:05 
GeneralRe: Countdown TimermemberSriram Chitturi17 May '07 - 9:52 
QuestionClock for VB.NET - aspx?memberTmuld15 Mar '07 - 8:13 
AnswerRe: Clock for VB.NET - aspx?memberSriram Chitturi17 May '07 - 9:55 
QuestionNewbie Trouble importing DigitalClock Control to VS 2005memberstabarinde17 Jan '07 - 11:09 
Questionmultiple clocksmemberlnae26 Sep '06 - 2:14 
AnswerRe: multiple clocksmemberskst31 Oct '06 - 5:44 
GeneralRe: multiple clocksmemberChristopherZ12 Jun '10 - 17:46 
GeneralClock disabledmemberjackieflan12 May '06 - 2:07 
GeneralSetting the timememberbighoss18 Apr '06 - 12:12 
QuestionIf i want to get the time shown on the your Control , what should i do?memberYour details have been updated6 Sep '05 - 18:34 
GeneralGMTmemberLee Becham8 Jul '05 - 9:54 
GeneralUsing two clocks at the same timemembercarlotino3 Jun '05 - 12:50 
GeneralRe: Using two clocks at the same timememberlnae14 Oct '06 - 21:38 
GeneralProblem with Digital.bmpmembercarlotino13 May '05 - 11:07 
GeneralRe: Problem with Digital.bmpmemberSriram Chitturi14 May '05 - 2:19 
GeneralRe: Problem with Digital.bmpmemberVinceHolmes28 Nov '05 - 5:49 
GeneralRe: Problem with Digital.bmpmemberpinturic5 Dec '05 - 23:17 
GeneralRe: Problem with Digital.bmpmemberpinturic5 Dec '05 - 23:27 
JokeRe: Problem with Digital.bmpmemberLillebrorDE23 Jan '06 - 1:33 
GeneralRe: Problem with Digital.bmpmemberJuba27 Dec '06 - 4:23 
GeneralPartially strange behaviormemberloizzi22 Apr '05 - 5:13 
QuestionCountDownDone...How to set Clocktime to 00:00:00?memberanarcho30 Nov '04 - 11:50 
GeneralSaved me some timememberTony D. Abel4 Nov '04 - 9:51 
GeneralSolid Functional Building BlockmemberFlashMerlot22 Oct '04 - 4:05 
GeneralNot revolutionary...memberRobert Rohde15 Feb '04 - 23:09 
GeneralRe: Not revolutionary...memberSriram Chitturi16 Feb '04 - 2:54 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 21 Feb 2004
Article Copyright 2004 by Sriram Chitturi
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid