Link to home
Start Free TrialLog in
Avatar of Quig653
Quig653

asked on

How to display a clock in Windows 8 that updates every second.

I am trying to use Visual Studio 2012 and Visual Basic, in Windows 8, to display a ticking clock, that updates every second, for a Metro Style App.

In previous versions this was easy to do by adding a Timer component and using the Tick event handler.  Timer components do not seem to be available in the Windows 8 versions.  

Can someone point me in the right direction?  I understand that there may be a basic shift in how Metro style apps handle timing events.  

I assume that once I understand how to display a ticking clock, that adding a stopwatch and countdown timer should be easy to figure out.
Avatar of plusone3055
plusone3055
Flag of United States of America image

Avatar of Quig653
Quig653

ASKER

I wish it were that simple...  There is no Timer control in Visual Studio 2012 for Windows 8.

Thanks for trying.
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Quig653

ASKER

Hey JB,

Thank you!  I quickly plugged in your answer and the clock is ticking away nicely.  I used LoadState because it was already there and handy.  I still need to try and understand a couple terms like, WithEvents and DispatcherTimer.  I don't think I've seen TimeSpan before either.  Now that I know what works, I can figure out why.

Thanks

-------------------------------------------------------------------------

Public NotInheritable Class MainPage
    Inherits Common.LayoutAwarePage

  Dim WithEvents timer As New Windows.UI.Xaml.DispatcherTimer

     Protected Overrides Sub LoadState(navigationParameter As Object, pageState As Dictionary(Of String, Object))

        timer.Interval = New TimeSpan(0, 0, 1)
        timer.Start()

    End Sub

    Private Sub Tick(sender As Object, e As RoutedEventArgs) Handles timer.Tick

        txtblkCurrentTime.Text = Date.Now.ToString("hh:mm:ss")

    End Sub
DispatcherTime is simply the name they gave to the Timer class used in XAML. It does a little more than a regular Timer, but since there does not seem to be a Timer class, I tried it and it worked the same way.

-----

WithEvents is the easy way to work with events. If you do not declare a variable WithEvents, you do not see it in the list at the top left of the code Window. This is not absolutely necessary to be able to work with events, but it helps a lot because you can work with the 2 lists to ask Visual Studio to create the event procedure for you.

If you where to declare the variable without the WithEvents, you would have to manually code the event procedure and then program a AddHandler call to link the procedure with your variable. WithEvents makes things easier.

In a regular Windows Appliction, if you look at the code that is generated by the form or page designer, you will see that there are declarations Friend WithEvents for each of the control you put on the page (Friend is used instead of Dim for security - it makes the variable visible only inside of the project so it cannot be accessed from another application that would link with yours). Without the WithEvents, the controls would not show in the leftmost ComboBox of the code editor, and the Handles at the end of event procedures would not do its job.

The mechanism is a little different for the controls on a Page in Windows Store applications, but the mechanism still holds in your VB code.

-----

While a Date variable holds Years, Months, Days, Hours, Minutes and Seconds values at a precise time, a TimeSpan does the same but for an elapsed length of time. As an example, if you substract from a Date (Ex: d = d.Substract(Date.Now)), the result is a TimeSpan that defines the time between d and the current date and time.

While the timers used in regular applications specify their Interval in milliseconds, the one provided with XAML requires a TimeSpan, which make more sense in my opinion. It's easier to define a duration of 2 days, 3 hours, 5 minutes by passing 2,3,5 than having to calculate how many milliseconds are in there. Intervals in milliseconds are also limited in length because they rapidly use up the bits required to hold the value. A TimeSpan is almost limitless.
Avatar of Quig653

ASKER

I appreciate your help and willingness to share.  

Thanks,
Q