Click here to Skip to main content
Licence CPOL
First Posted 27 Nov 2008
Views 21,435
Downloads 998
Bookmarked 17 times

Digital Clock in SilverLight 2

By Mohammad Dayyan | 27 Nov 2008
How to create a simple dgital clock in SilverLight 2.

1
1 vote, 12.5%
2
1 vote, 12.5%
3
2 votes, 25.0%
4
4 votes, 50.0%
5
4.24/5 - 8 votes
μ 4.24, σa 2.02 [?]

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

About the Author

Mohammad Dayyan



Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberDurgaprasadtb4:52 26 Dec '10  
GeneralMy Vote 4 PinmemberKunalChowdhury8:54 27 Feb '10  
GeneralThanks! Pinmembervigh2:39 25 Feb '09  
GeneralMy vote of 1 Pinmemberberzie20:30 24 Jan '09  
GeneralMy vote of 2 PinmemberProJester111:14 27 Nov '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 27 Nov 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid