Posts By Category

Posts By Date

Resources:

C# Books
ASP.NET Books DotNet4All








If you like to support this site, feel free to make a donation to support improvements.

Thank you!

Monetize Your Blog

Use .NET Timers to setup a Clock user control on a Windows Form in C#

Making your own Clock control to display on your Windows Form to display and update the current time up to the second is an easy deal using C# Timers.

Creating The Clock User Control Step By Step

Here is one way to do it:

  • Open Visual C# 2005 Express and start a Class Library Project.
  • Add a User Control to the project.
  • From the Tool Box, add a Label to the User Control.
  • From the Tool Box, add a Timer to the User Control.
  • Double click on the timer control and upon each tick implement the following code:

private void tmrRefresh_Tick( object sender, EventArgs e)

{

lblClockText.Text =

DateTime .Now.ToLongTimeString();

}

  • Now double click on the User Control and in the Load event, start the timer as follows:

private void Clock_Load( object sender, EventArgs e)

{

tmrRefresh.Start();

}

Using the Clock Control in your Windows Form:

Your Clock control is ready to use in any project now! Create a regular Windows Application project from Visual C# Express, add your user control to the Tool box to as follows:

  • Right click on the Tool Box and choose Add Tab, name the new tab for example: "My Controls"
  • Right click on "My Controls" tab and choose "Choose Items", then browse to the Clock User Control DLL you created earlier, and add it.
  • Now that you have the Clock Control in the Tool Box, just drag and drop it into your Windows Form, and press Ctrl-F5, and you should see your Form with a Clock on it!

Conclusion:

The basic idea is simple: "Utilize a Timer object to refresh a Lable Text property with the current time".

Source Code:

Here is the full source code:

Clock Control

kick it on DotNetKicks.com

Feedback

Posted on 4/12/2012 12:18:44 PM

Thanks a lot.
Like!

Posted on 12/20/2011 1:07:39 AM

THANKS

Posted on 11/4/2011 9:33:25 AM

Thank you! Simple and effective.

Posted on 9/28/2011 5:26:47 AM

Nice One.
Thank You Very Much

Posted on 8/16/2011 3:37:14 PM

excelent, basic and easy

Posted on 8/2/2011 8:51:42 PM

i liked the way u explained, thanks a lot.

Posted on 2/25/2011 6:31:50 AM

Hi...
Loads of thanks to you.. U saved my time and space of coding alot
Such a short and workable code..
Thanks for sharing..
Keep this on..
All the best...

Posted on 2/15/2011 11:28:29 AM

This code is very very good and help full. Lots lots lots of thanks.

Posted on 2/8/2011 2:36:23 AM

Thanks its really helpful to me.

Posted on 11/9/2010 5:09:22 AM

thanku so much....
short and sweet code creating a clock control

Posted on 5/28/2010 3:18:03 AM

i liked the way u explained

Posted on 2/4/2010 10:37:27 AM

how to create clock for c# programin

Posted on 10/10/2008 6:34:01 AM

I liked the process to explain how to apply user control in Windows form.....................

thanks and regards
Pradip Shinde
Software Engineer
Amyo Technologies Pvt.Ltd.

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 


Copyright © 2007 Yousef Mannaa. All material on this site is copyrighted.
Do not publish or reproduce any of this material without written permission from the Author