Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / XAML

Digital Clock in SilverLight 2

Rate me:
Please Sign up or sign in to vote.
4.24/5 (8 votes)
27 Nov 2008CPOL 50.6K   2.6K   19   5
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.

C#
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:

XML
<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:

C#
// 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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Durgaprasad Budhwani26-Dec-10 3:52
Durgaprasad Budhwani26-Dec-10 3:52 
Quick and easy..
thats what i wanted Smile | :)
GeneralMy Vote 4 Pin
Kunal Chowdhury «IN»27-Feb-10 7:54
professionalKunal Chowdhury «IN»27-Feb-10 7:54 
GeneralThanks! Pin
vigh25-Feb-09 1:39
vigh25-Feb-09 1:39 
GeneralMy vote of 1 Pin
berzie24-Jan-09 19:30
berzie24-Jan-09 19:30 
GeneralMy vote of 2 Pin
ProJester127-Nov-08 10:14
ProJester127-Nov-08 10:14 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.