Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Digital Clock in SilverLight 2

0.00/5 (No votes)
27 Nov 2008 1  
How to create a simple dgital clock in SilverLight 2.

SLdigitalClock.jpg

Introduction

This tutorial shows you how we to create a simple digital clock in SilverLight 2. I've also created an analog clock in WPF and Expression Blend 2.

Using the code

Before everything, we need to create a timer. I learned to do this from here.

public void StartTimer(object o, RoutedEventArgs sender)
{
    System.Windows.Threading.DispatcherTimer myDispatcherTimer = 
        new System.Windows.Threading.DispatcherTimer();
    myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1000); // 1000 Milliseconds
    myDispatcherTimer.Tick += new EventHandler(Each_Tick);
    myDispatcherTimer.Start();
}

Now, we should create three TextBlocks with XAML code:

<TextBlock Margin="8,8,0,9" Foreground="#FFFFFFFF" 
    TextWrapping="Wrap" HorizontalAlignment="Left" 
    FontSize="72" Text="00" 
    d:LayoutOverrides="HorizontalAlignment, 
    Height" VerticalAlignment="Center" x:Name="hourText"/>

Now, we write a method to change the text every second:

// Fires every 1000 miliseconds while the DispatcherTimer is active.
public void Each_Tick(object o, EventArgs sender)
{
    hourText.Text = DateTime.Now.Hour.ToString();
    minuteText.Text = DateTime.Now.Minute.ToString();
    secondText.Text = DateTime.Now.Second.ToString();
}

History

  • 27th November, 2008: First post.

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