private void SecondTimer_Tick(object sender, EventArgs e) { //Update internal counters secondsElapsed++; secondsRemaining = secondsCountdownSetTo - secondsElapsed; //Update UI UpdateCounterDisplays(); int setTo = secondsElapsed * 100 / secondsCountdownSetTo; while (setTo > 100) //handle looping { setTo -= 100; } PBar.Value = setTo; //Check for the timer being finished if (secondsCountdownSetTo == secondsElapsed) { if (ChkLoop.Checked) //restarting loop { //TimeLeftGroup.Enabled = false; if (ChkNotifiy.Checked) { notifyIcon1.BalloonTipTitle = "Starting Over"; notifyIcon1.BalloonTipText = "The requested duration has passed." + " The timer will start over again, because it is in \u201CLoop\u201D mode." + " \n(To disable these alerts, uncheck \u201CAlert me " + "from the notification area.\u201D)"; notifyIcon1.ShowBalloonTip(8000); } //Reset seconds elapsed counter secondsElapsed = 0; } else //ending { //Write message to top of window Information.Text = "Countdown complete! Finished after " + TimeHandler.FormatTime(secondsElapsed); //Stop counting StopTimer(); //Show window if hidden this.Show(); //Alert if user desires if (ChkNotifiy.Checked) { notifyIcon1.BalloonTipTitle = "Time\u2019s Up!"; notifyIcon1.BalloonTipText = "The timer has finished the countdown from " + TimeHandler.FormatTime(secondsCountdownSetTo, true) + "."; notifyIcon1.ShowBalloonTip(240000); } } } notifyIcon1.Text = "CustomTimer \n" + PBar.Value + "% of " + TimeHandler.FormatTime(secondsCountdownSetTo) + "\n" + TimeHandler.FormatTime(secondsElapsed) + " elapsed" + "\n" + TimeHandler.FormatTime(secondsRemaining) + " remaining"; }
private void BtnReset_Click(object sender, EventArgs e) { Information.Text = "Timer stopped with " + TimeHandler.FormatTime(secondsElapsed) + " elapsed and " + TimeHandler.FormatTime(secondsRemaining) + " remaining."; StopTimer(); ResetTimer(); }
private void UpdateCounterDisplays() { if (secondsRemaining >= 0) { TimeUnits remaining = TimeHandler.ConvertSecondsToTimeUnits(secondsRemaining); String secRemaining = remaining.Seconds.ToString(); LblSeconds.Text = secRemaining.PadLeft(2, '0'); String minRemaining = remaining.Minutes.ToString(); LblMinutes.Text = minRemaining.PadLeft(2, '0'); LblHours.Text = remaining.Hours.ToString(); LblDays.Text = remaining.Days.ToString(); } else { LblSeconds.Text = "00"; LblMinutes.Text = "00"; LblHours.Text = "0"; LblDays.Text = "0"; } LblElapsed.Text = TimeHandler.FormatTime(secondsElapsed); LblTotal.Text = TimeHandler.FormatTime(secondsCountdownSetTo); }
/*private void ResetMultiButton() * { * BtnMulti.Text = "Start"; * } * * public void DisableButton(Button button) * { * button.Text = "Timing!"; * button.Enabled = false; * }*/ private void BtnMulti_Click(object sender, EventArgs e) { this.Text = "CustomTimer"; //Clear the "Welcome" from title bar if (SecondTimer.Enabled || BtnMulti.Text == "Resume") { PauseTimer(); //Pause or un-pause } else { //Set timer to proper duration, etc. //We will take the user's input string and extract the //desired timer length from this. //Define vars int totalSeconds = 0; //Total seconds to countdown; sum of days, hours, min, sec. TimeUnits timeUnits = new TimeUnits(); //Will fill from user input String duration = CboDuration.Text; //The user's time string (e.g. 5:00) Match match = Regex.Match("d", "c"); //initialize to unsuccessful //Define possible input formats (using regular expresssions and named groups) String[] patterns = { @"^\s*((((?<hours>\d{1,4})\s*:\s*)?(?<minutes>[012345]?\d)\s*)?:\s*)?(?<seconds>[012345]?\d)\s*$", @"^((?<days>[0123]?\d{1,2})\s*(d|days?)\s*,?\s*(and\s*)?)?((?<hours>[012345]?\d)\s*(h|hrs?|hours?)\s*,?\s*(and\s*)?)?((?<minutes>[012345]?\d)\s*(m|mins?|minutes?)\s*,?\s*(and\s*)?)?((?<seconds>[012345]?\d)\s*(s|secs?|seconds?))?\s*$" }; //figure out user input foreach (String pattern in patterns) { match = Regex.Match(duration, pattern, RegexOptions.IgnoreCase); if (match.Success) { break; } } if (match.Success) { if (match.Groups["seconds"].Success) { timeUnits.Seconds = Int32.Parse(match.Groups["seconds"].Value); } if (match.Groups["minutes"].Success) { timeUnits.Minutes = Int16.Parse(match.Groups["minutes"].Value); } if (match.Groups["hours"].Success) { timeUnits.Hours = Int16.Parse(match.Groups["hours"].Value); } if (match.Groups["days"].Success) { timeUnits.Days = Int16.Parse(match.Groups["days"].Value); } //Clear out any data from a previous run ResetTimer(); //calculate total seconds runtime totalSeconds = TimeHandler.ConvertTimeUnitsToSeconds(timeUnits); //avoid zero-length intervals if (totalSeconds < 1) { totalSeconds = 60; //one minute timeUnits.Minutes = 1; //because we simultaneously have this value in seconds and TU form } //Set the counter variables to starting position secondsCountdownSetTo = totalSeconds; secondsRemaining = secondsCountdownSetTo; //Update UI to reflect changes Information.Text = "Timer has been set to " + TimeHandler.FormatTime(timeUnits, true) + ". \nTimer started."; UpdateCounterDisplays(); //Start Timer StartTimer(); } else { MessageBox.Show("Unfortunately, you did not enter a recognizeable " + "length of time, or else entered a time longer than the maximum " + "(9999 hours, 59 minutes, and 59 seconds)."); } } }