locked
Develop a count down on Windows Store C# RRS feed

  • Question

  • Hello Everybody !!

    I need to implement a count down in my windows store apps.

    I need to have this kind of count down :

    - At the beginning : 01:40:00 and in the end : 00:00:00

    Could you please help me in giving me a sample of code?

    Thanks very much

    Sunday, May 10, 2015 9:02 PM

All replies

  • What exactly are you looking for? What have you tried?

    You can use a DispatcherTimer to track the time as it passes and can bind the resultant time to whatever display you'd like.

    Sunday, May 10, 2015 10:22 PM
    Moderator
  • Hello Rob!

    Thanks for your answer.

    In fact, I need to do a count down like for exams.

    For example when user starts exam, the display time is : 01:40:00

    And each second, the display time is the begining time less 1 second like :

    - 01:39:59

    - 01:39:58

    ...

    - 01:02:47

    - 01:02:46

    ...

    In the end the display time is : 00:00:00

    Is that more clear?

    Thank you

    Monday, May 11, 2015 9:17 AM
  • Hello DiddyRennes,

    Here I use the solution from Rob and give you a sample about this issue:

    XAML:

    <Page
        x:Class="CountDownApp.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:CountDownApp"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <StackPanel>
                <TextBox x:Name="CountDownText" FontSize="35" Height="200" Margin="275,0,303,0"/>
                <Button Content="Start" Height="58" Width="179" Click="Start_Click"/>
                <Button Content="Submit" Height="58" Width="179" Click="Submit_Click"/>
            </StackPanel>
        </Grid>
    </Page>
    

    Backgroud:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices.WindowsRuntime;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Popups;
    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;
    
    // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace CountDownApp
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class MainPage : Page
        {
            DispatcherTimer dispatcherTimer;
            int seconds;
    
            public MainPage()
            {
                this.InitializeComponent();
                dispatcherTimer = new DispatcherTimer();
                dispatcherTimer.Tick += dispatcherTimer_Tick;
                dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
                
    
            }
    
            private void dispatcherTimer_Tick(object sender, object e)
            {
                seconds--;
                TimeSpan span1 = new TimeSpan(0, 0, seconds);
                //Convert timespan to string
                CountDownText.Text=string.Format("{0:00}:{1:00}:{2:00}",span1.Hours,span1.Minutes,span1.Seconds);
                if (seconds == 0)
                {
                    dispatcherTimer.Stop();
                }
            }
    
         
    
            private void Start_Click(object sender, RoutedEventArgs e)
            {
                dispatcherTimer.Start();
              
            }
    
            private void Submit_Click(object sender, RoutedEventArgs e)
            {
                //Convert Text to TimeSpan and get totalseconds, its double and need to be convert to int because milliseconds
                //Here you need to handle exception
                    TimeSpan ts = TimeSpan.Parse(CountDownText.Text);
                    seconds = Convert.ToInt32(ts.TotalSeconds);
               
            }
        }
    }
    

    I haven't handle the exceptions but I think you can handle them. In my sample I use submit button to submit the time from user input, then use start to start the CountDown.

    Best regards,

    Tuesday, May 26, 2015 2:38 AM