Click here to Skip to main content
15,886,622 members
Articles / Desktop Programming / WPF

A WPF Digital Clock

Rate me:
Please Sign up or sign in to vote.
4.00/5 (13 votes)
26 Apr 2008CPOL2 min read 114.4K   9.8K   49  
A WPF Digital Clock for displaying the current time, styled using RibbonStyleHandler
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace DNBSoft.WPF.Clocks
{
    /// <summary>
    /// Interaction logic for DigitalClock.xaml
    /// </summary>
    public partial class DigitalClockControl : UserControl
    {
        protected delegate void RefreshDelegate();
        private Thread updateThread = null;
        private DateTime currentTime = DateTime.Now;

        public DigitalClockControl()
        {
            InitializeComponent();

            this.AutoUpdate = true;
        }

        #region styling
        public Brush DigitBrush
        {
            set
            {
                p0.RenderBrush = value;
                p1.RenderBrush = value;
                p3.RenderBrush = value;
                p4.RenderBrush = value;
                p6.RenderBrush = value;
                p7.RenderBrush = value;
            }
        }

        public Brush DotBrush
        {
            set
            {
                p2.RenderBrush = value;
                p5.RenderBrush = value;
            }
        }

        public Brush ClockBackground
        {
            get
            {
                return masterBorder.Background;
            }
            set
            {
                masterBorder.Background = value;
            }
        }
        #endregion

        public DateTime CurrentTime
        {
            get
            {
                return currentTime;
            }
            set
            {
                currentTime = value;

                #region hours
                if (value.Hour > 9)
                {
                    p0.Value = int.Parse(value.Hour.ToString()[0].ToString());
                    p1.Value = int.Parse(value.Hour.ToString()[1].ToString());
                }
                else
                {
                    p0.Value = 0;
                    p1.Value = int.Parse(value.Hour.ToString()[0].ToString());

                }
                #endregion

                #region minutes
                if (value.Minute > 9)
                {
                    p3.Value = int.Parse(value.Minute.ToString()[0].ToString());
                    p4.Value = int.Parse(value.Minute.ToString()[1].ToString());
                }
                else
                {
                    p3.Value = 0;
                    p4.Value = int.Parse(value.Minute.ToString()[0].ToString());

                }
                #endregion

                #region seconds
                if (value.Second > 9)
                {
                    p6.Value = int.Parse(value.Second.ToString()[0].ToString());
                    p7.Value = int.Parse(value.Second.ToString()[1].ToString());
                }
                else
                {
                    p6.Value = 0;
                    p7.Value = int.Parse(value.Second.ToString()[0].ToString());

                }
                #endregion
            }
        }

        public bool AutoUpdate
        {
            get
            {
                return updateThread != null;
            }
            set
            {
                if (updateThread != null)
                {
                    try
                    {
                        updateThread.Abort();
                    }
                    catch (Exception)
                    { }
                }

                if (value)
                {
                    updateThread = new Thread(delegate()
                    {
                        try
                        {
                            while (true)
                            {
                                DateTime d = DateTime.Now;

                                bool complete = false;
                                this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new RefreshDelegate(delegate()
                                {
                                    this.CurrentTime = d;

                                    complete = true;
                                }));

                                #region pause
                                try
                                {
                                    do
                                    {
                                        Thread.Sleep(900);
                                    }
                                    while (!complete);
                                }
                                catch (Exception)
                                {
                                }
                                #endregion
                            }
                        }
                        catch (Exception)
                        {
                        }
                    });
                    updateThread.Name = "Clock Thread";
                    updateThread.Start();
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Rail Research UK
United Kingdom United Kingdom
I originally studied for a masters in engineering of software engineering at The University of Birmingham during 2000-2004, of which I received a 2:1. I continued at Birmingham University working with Civil Engineering and Rail Research UK where I am currently in my final year of a 3 year PhD project developing a Computational Intelligent Approach to Railway Intervention Planning. Although my work has a significant focus on railway engineering and associated practices much of my work is with data mining (on SQL Server 2008) and computational intelligence (CI) techniques. My key areas of expertise in CI are clustering algorithms (including Rival Penalised Competitive Learning) and evolutionary algorithms.

Outside of my formal work I enjoy testing the latest technologies such as .NET 3.5 and the many frameworks of which it comprises (mainly WPF). I have several projects on the go including a .NET and DirectX port of Quake 3 and many utility libraries. I also maintain an extensive website coded in Cold Fusion which is regularly updated; more information is available about me there.

Comments and Discussions