Ping Blog Timer demo in Windows 8 Application using C# | Easy Techs!

Pages

Wednesday, December 19, 2012

Timer demo in Windows 8 Application using C#

I needed a timer to pop in one of my windows 8 applications and I tried to get sample codes on the web. The codes in there were helpful but way too difficult to track down. So, I thought of writing a small code snippet of my own and sharing it to whoever it may concern to!
In this little code snippet, the user is supposed to input any random text in the text box input that appears on the UI with in 5 seconds or else the UI tells the time is up! I have not used any pop us or any logical scenario implementation in here. The whole idea of this is to help you understand how you can implement the timer into your project. Once you get a hold of the concept, I am sure you have your own 'logical scenario of implementation' that is far more important.
Here is the XAML code for the UI design.

<Page
    x:Class="timerDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:timerDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <TextBox x:Name="inputBox" HorizontalAlignment="Left" Height="40" Margin="110,239,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="248" FontSize="24"/>
        <TextBlock x:Name="timeRemaining" HorizontalAlignment="Left" Margin="751,87,0,0" TextWrapping="Wrap" Text="Time Left = " VerticalAlignment="Top" Height="46" Width="151" FontSize="26"/>
        <TextBox x:Name="showTime" HorizontalAlignment="Left" Margin="907,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
        <TextBlock x:Name="outputBox" HorizontalAlignment="Left" Margin="110,194,0,0" TextWrapping="Wrap" Text="Your Input Here" VerticalAlignment="Top" Height="40" Width="378" FontSize="24"/>
        <Button x:Name="clickButton" Content="Click Me!" HorizontalAlignment="Left" Margin="163,336,0,0" VerticalAlignment="Top" Click="clickButton_Click" />
    </Grid>
</Page>
This code simply creates two text boxes, two text blocks and a button, like the one shown in the figure:

Here is the code required for the respective .cs file

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace timerDemo
{
    public sealed partial class MainPage : Page
    {
        private int timeLeft;
        private DispatcherTimer timer;
        public MainPage()
        {
            timeLeft = 5;
            this.InitializeComponent();
            startMyTimer();
        }
        /// <summary>
        /// this method here starts the timer
        /// </summary>
        private void startMyTimer()
        {
            if (this.timer != null)
            {
                this.timer.Stop();
                this.timer = null;
            }
            //create a new DispatcherTimer type of object
            timer = new DispatcherTimer();
            //set the interval between two timer ticks. Here it 1 s.
            //the format is hr.min.sec
            //if you put (0,2,0) your timer will change in every two minutes
            timer.Interval = new TimeSpan(0, 0, 1);
            //this one here is event
            timer.Tick += timer_tick;
            //kick off the timer
            timer.Start();
           
        }
        public void timer_tick(object sender, object args)
        {
            if (timeLeft > 0)
            {
                timeLeft -= 1;
                this.showTime.Text = Convert.ToString(timeLeft);
            }
            else
            {
                timer.Stop();
                //check if the time is up!
                outputBox.Text = "Your Time is up!";
            }
        }
        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
        private void clickButton_Click(object sender, RoutedEventArgs e)
        {
            //check after the button is ticked
            if (timeLeft == 0 && inputBox.Text == "")
                outputBox.Text = "You are out of time";
            else
                outputBox.Text = "You have supplied input in time";
        }
      
    }
}

0 comments:

Post a Comment