Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows time TimeSpan Metro , +
Hello, i need to build an app that use a countdown (Years, Days, Hour, Minutes, Seconds). I've tried to write a code and i succeed to calculate the time, but when i try to put this time as a string in a texblock, he is not updating. Can anyone help me to set the textblock to update himself ?
Notice: I'm a beginner in C# and Metro App's. And sorry for my bad english.
 
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 Christmas_Countdown
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
 
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }
 
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
 
            DispatcherTimer t = new DispatcherTimer();
            DateTime endtime = new DateTime(2012, 12, 25, 0, 0, 0);
            DateTime now = DateTime.Now;
            t.Interval = endtime - now;
            TimeSpan ts = t.Interval;
            t.Start();
 
            string countDown = string.Format("{0} Days, {1} Hours, {2} Minutes, {3} Seconds til Christmas.", t.Interval.Days, t.Interval.Hours, t.Interval.Minutes, t.Interval.Seconds);
            Day_TxtBlk.Text = countDown;
        }
 
        private void Day_TxtBlk_Loaded(object sender, RoutedEventArgs e)
        {
            
        }
 

 
    }
}
Posted 13-Dec-12 0:11am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I found the solution to my problem.
 
void t_Tick(object sender, object e)
        {
            TimeSpan ts = endtime.Subtract(DateTime.Now);
 
            string countdowndays = ts.Days.ToString();
            daytxtblk.Text = countdowndays;
 
            string countdownhours = ts.Hours.ToString();
            hourstxtblk.Text = countdownhours;
 
            string countdownminutes = ts.Minutes.ToString();
            minutetxtblk.Text = countdownminutes;
 
            string countdownseconds = ts.Seconds.ToString();
            sectxtblk.Text = countdownseconds;
        }
 

        private void christmas_2012_Click(object sender, RoutedEventArgs e)
        {
            
 
            if (this.IsEnabled)
            {
                txtboxchr.Visibility = Visibility.Visible;
                txtboxhours.Visibility = Visibility.Visible;
                txtboxminutes.Visibility = Visibility.Visible;
                txtboxseconds.Visibility = Visibility.Visible;
 
                daytxtblk.Visibility = Visibility.Visible;
                hourstxtblk.Visibility = Visibility.Visible;
                minutetxtblk.Visibility = Visibility.Visible;
                sectxtblk.Visibility = Visibility.Visible;
            }
           
 
            t = new DispatcherTimer();
            endtime = new DateTime(2012, 12, 25, 0, 0, 0);
            
            if (DateTime.Now.Year > endtime.Year)
            {
                t.Stop();
                txtboxchr.Visibility = Visibility.Collapsed;
                txtboxhours.Visibility = Visibility.Collapsed;
                txtboxminutes.Visibility = Visibility.Collapsed;
                txtboxseconds.Visibility = Visibility.Collapsed;
 
                daytxtblk.Visibility = Visibility.Collapsed;
                hourstxtblk.Visibility = Visibility.Collapsed;
                minutetxtblk.Visibility = Visibility.Collapsed;
                sectxtblk.Visibility = Visibility.Collapsed;
                pleasesetdat();
            }
            else 
            {
                t.Interval = new TimeSpan(1000);
                TimeSpan ts = endtime.Subtract(DateTime.Now);
                t.Tick += t_Tick;
                t.Start();
            }
        }
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 505
1 Maciej Los 309
2 BillWoodruff 174
3 /\jmot 160
4 Suraj Sahoo | Coding Passion 160
0 OriginalGriff 8,654
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,229
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 16 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100