Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Need help with alarm clock using WPF please...

$
0
0

hello i am working on a timer/alarm clock using WPF Visual Studio 2012 and i've gotten a little stuck. what i have so far works and i'm happy with it, the problem is i want the user to be able to adjust the time up or down using buttons to advance or decrease the time to there liking... so far it is hard coded with a set time.

here is what i have so far:

public partial class myTimerControl : UserControl
    {
        private int time = 20;   ///This is my hard coded set time.
        private DispatcherTimer timer;

        public myTimerControl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            btnAddHour.Visibility = System.Windows.Visibility.Hidden;
            btnAddMin.Visibility = System.Windows.Visibility.Hidden;
            btnAddSec.Visibility = System.Windows.Visibility.Hidden;
            btnSubMin.Visibility = System.Windows.Visibility.Hidden;
            btnSubSec.Visibility = System.Windows.Visibility.Hidden;
            btnSubHour.Visibility = System.Windows.Visibility.Hidden;
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Tick += timer_Tick;
            timer.IsEnabled = true;
            btnSetTime.IsEnabled = false;
            timer.Start();   
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (time > 10)
            {
                time--;
                tbCountdown.Text = string.Format("00:0{0}:{1}", time / 60, time % 60);
            }
            else
            {
                if (time > 0)
                {
                    time--;
                    tbCountdown.Foreground = Brushes.Crimson;
                    tbCountdown.Text = string.Format("00:0{0}:0{1}", time / 60, time % 60);
                }
                else
                {
                    timer.Stop();
                    MessageBox.Show("Boom");
                    return;
                }
            }
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (timer.IsEnabled == true)
                {
                    timer.IsEnabled = false;
                    timer.Stop();

                }
                else
                {
                    timer.IsEnabled = true;
                    timer.Start();
                }
            }
            catch
            {
                MessageBox.Show("There is nothing to pause.");
                return;
            }
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                timer.IsEnabled = false;
                timer.Stop();
                timer = null;
                btnSetTime.IsEnabled = true;
            }
            catch
            {
                MessageBox.Show("Time is already stopped.");
                return;
            }
        }

        private void btnSetTime_Click(object sender, RoutedEventArgs e)
        {
            btnAddHour.Visibility = System.Windows.Visibility.Visible;
            btnAddMin.Visibility = System.Windows.Visibility.Visible;
            btnAddSec.Visibility = System.Windows.Visibility.Visible;
            btnSubMin.Visibility = System.Windows.Visibility.Visible;
            btnSubSec.Visibility = System.Windows.Visibility.Visible;
            btnSubHour.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnAddHour_Click(object sender, RoutedEventArgs e)
        {
            ///Here i would like the user to be able to increase the time they want by 1 hour each time they press this button
        }

        private void btnAddMin_Click(object sender, RoutedEventArgs e)
        {
            ///Here i would like the user to be able to increase the minutes by 1 minutes each time they press this button
        }

        private void btnAddSec_Click(object sender, RoutedEventArgs e)
        {
            ///Same concept with the seconds
        }

        private void btnSubHour_Click(object sender, RoutedEventArgs e)
        {
            ///Here i would like the user to be able tosubtract an hour from the time they select, in case they go over or something...
        }

        private void btnSubMin_Click(object sender, RoutedEventArgs e)
        {
           ///Same thing with minutes... subtract....
        }

        private void btnSubSec_Click(object sender, RoutedEventArgs e)
        {
          ///Same thing with the seconds....subtract...
        }
    }
}

So this is where i'm stuck. i hope someone can show/ guide me in the right direction. thanks for any help i appreciate it.


Adam



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>