Click here to Skip to main content
6,306,412 members and growing! (17,175 online)
Email Password   helpLost your password?
Desktop Development » Selection Controls » Date/Time controls     Intermediate

Multipurpose Digital Clock Control using .NET

By Sriram Chitturi

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.
C#.NET 1.0, .NET 1.1, Win2K, WinXPVS.NET2003, Dev
Posted:15 Feb 2004
Updated:20 Feb 2004
Views:116,325
Bookmarked:72 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
34 votes for this article.
Popularity: 6.17 Rating: 4.03 out of 5
4 votes, 11.8%
1
3 votes, 8.8%
2
2 votes, 5.9%
3
7 votes, 20.6%
4
18 votes, 52.9%
5

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


Member

Occupation: Architect
Company: Philegance LLC
Location: United States United States

Other popular Selection Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 34 (Total in Forum: 34) (Refresh)FirstPrevNext
GeneralCompact Framework? PinmemberPengie15:22 4 Dec '08  
QuestionAlarm Set help Pinmemberjackchow15:19 13 Nov '08  
Generalvb 2005? Pinmembergaryjohn_200012:56 6 Sep '07  
QuestionStart time of Stopwatch Pinmemberfightclub77720:56 17 Jul '07  
QuestionLicense? PinmemberKcodeProj6:54 5 Jul '07  
AnswerRe: License? PinmemberSriram Chitturi7:02 5 Jul '07  
GeneralRe: License? PinmemberKcodeProj23:45 5 Jul '07  
GeneralClock Control Freeze Vales PinmemberMark Gales13:38 30 Jun '07  
GeneralCountdown Timer PinmemberBrizee0:05 17 May '07  
GeneralRe: Countdown Timer PinmemberSriram Chitturi10:52 17 May '07  
QuestionClock for VB.NET - aspx? PinmemberTmuld9:13 15 Mar '07  
AnswerRe: Clock for VB.NET - aspx? PinmemberSriram Chitturi10:55 17 May '07  
QuestionNewbie Trouble importing DigitalClock Control to VS 2005 Pinmemberstabarinde12:09 17 Jan '07  
Questionmultiple clocks Pinmemberlnae3:14 26 Sep '06  
AnswerRe: multiple clocks Pinmemberskst6:44 31 Oct '06  
GeneralClock disabled Pinmemberjackieflan3:07 12 May '06  
GeneralSetting the time Pinmemberbighoss13:12 18 Apr '06  
QuestionIf i want to get the time shown on the your Control , what should i do? PinmemberYour details have been updated19:34 6 Sep '05  
GeneralGMT PinmemberLee Becham10:54 8 Jul '05  
GeneralUsing two clocks at the same time Pinmembercarlotino13:50 3 Jun '05  
GeneralRe: Using two clocks at the same time Pinmemberlnae22:38 14 Oct '06  
GeneralProblem with Digital.bmp Pinmembercarlotino12:07 13 May '05  
GeneralRe: Problem with Digital.bmp PinmemberSriram Chitturi3:19 14 May '05  
GeneralRe: Problem with Digital.bmp PinmemberVinceHolmes6:49 28 Nov '05  
GeneralRe: Problem with Digital.bmp Pinmemberpinturic0:17 6 Dec '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Feb 2004
Editor: Smitha Vijayan
Copyright 2004 by Sriram Chitturi
Everything else Copyright © CodeProject, 1999-2009
Web16 | Advertise on the Code Project