Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I can make the app count down minutes and seconds

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace count_down
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private int time = 30;
        private DispatcherTimer timer; 
        public MainWindow()
        {
            InitializeComponent();
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0,0,1);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        public void Timer_Tick(object sender, EventArgs e)
        {
            if (time > 0)
            {

                if (time <= 20)
                {
                    if (time % 2 == 0)
                    {
                        tbCountDown.Foreground = Brushes.Orange;
                    }
                    else
                    {
                        tbCountDown.Foreground = Brushes.Orange;

                    }

                    if (time <= 10)
                    {
                        if (time % 2 == 0)
                        {
                            tbCountDown.Foreground = Brushes.Red;
                        }
                        else
                        {
                            tbCountDown.Foreground = Brushes.White;

                        }
                    }
                
                    time--;
                    tbCountDown.Text = string.Format("00:0{0}:{1}", time / 60, time % 60);
                }
                else
                {
                    time--;
                    tbCountDown.Text = string.Format("00:0{0}:{1}", time / 60, time % 60);

                }

            }
            else
            {
                timer.Stop();
                MessageBox.Show("BOOM!");
            }
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {

            if (txtB.Text == string.Empty)
            {
                MessageBox.Show("Please enter value");
            }
            else
            {
                MessageBox.Show(txtB.Text);
            }
        }
    }
}
Posted
Updated 3-Nov-16 7:38am

1 solution

You can create a TimeSpan that includes a number of days: see TimeSpan Constructor (Int32, Int32, Int32, Int32) (System)[^].
 
Share this answer
 
Comments
#realJSOP 3-Nov-16 14:56pm    
TimeSpan doesn't count months.
Richard MacCutchan 3-Nov-16 14:57pm    
But you can calculate the number of days required from the months.
Member 12717000 5-Nov-16 16:45pm    
Thanks for the speedy reply, could you help by showing the timespan code application and the actual print line (tbCountDown.Text = string.Format("00:0{0}:{1}, time / 60, time % 60);
Richard MacCutchan 6-Nov-16 2:50am    
Sorry, I don't understand the question. The TimeSpan.ToString() method allows you to display the values in any format that you want.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900