/// <summary> /// Checks user input before further processing /// </summary> /// <returns>Result of checks</returns> private bool RunChecks() { ExceptionHandler.LogEvent("[Menu] Running checks..."); bool errTracker = true; // if anything goes wrong the tracker will be set to false string errMessage = null; // error messages will append to this // Check if chosen action is a valid option if (!actionComboBox.Items.Contains(actionComboBox.Text)) { errTracker = false; errMessage += "Please select a valid action from the dropdown menu!\n\n"; } // Check if all time values are zero if (hoursNumericUpDown.Value == 0 && minutesNumericUpDown.Value == 0 && secondsNumericUpDown.Value == 0) { errTracker = false; errMessage += "The timer cannot start at 0!\n\n"; } // Try to build and convert a the values to a TimeSpan and export it as a string. try { Numerics.ConvertTimeSpanToString(new TimeSpan(Convert.ToInt32(hoursNumericUpDown.Value), Convert.ToInt32(minutesNumericUpDown.Value), Convert.ToInt32(secondsNumericUpDown.Value))); } catch { errTracker = false; errMessage += "TimeSpan conversion failed! Please check if your time values are within a reasonable range.\n\n"; } // Sanity check try { TimeSpan ts = new TimeSpan(Convert.ToInt32(hoursNumericUpDown.Value), Convert.ToInt32(minutesNumericUpDown.Value), Convert.ToInt32(secondsNumericUpDown.Value)); if (ts.TotalDays > 100) { MessageBox.Show($"Your chosen time equates to {Math.Round(ts.TotalDays)} days ({Math.Round(ts.TotalDays / 365, 2)} years)!\n" + "It is highly discouraged to choose such an insane amount of time as either your hardware, operating system, or this is app will fail *way* before you even come close to reaching the target!" + "\n\nBut if you are actually going to do this, please tell me how long this app survived.", "This isn't Stargate and your PC won't stand the test of time!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch { } ExceptionHandler.LogEvent("[Menu] Ran checks: " + errTracker.ToString()); checkResult = errMessage; return(errTracker); }
/// <summary> /// Updates the time label with current time left and applies the corresponding background color. /// </summary> private void UpdateUI(TimeSpan ts) { if (Math.Round(lastStateUITimeSpan.TotalSeconds) != Math.Round(ts.TotalSeconds) || lastStateUITimeSpan.TotalSeconds <= 0 || lastStateUIFormWindowState != WindowState) // Only update if the seconds from the TimeSpan actually changed and when it first started { // Save current data to last state memory lastStateUITimeSpan = ts; lastStateUIFormWindowState = WindowState; // Update time labels string elapsedTime = Numerics.ConvertTimeSpanToString(ts); timeLabel.Text = elapsedTime; timeMenuItem.Text = elapsedTime; if (UI) // UI for countdown window { this.Text = "Countdown"; // Decide which color/animation to use if (ts.Days > 0 || ts.Hours > 0 || ts.Minutes >= 30) { BackColor = Color.ForestGreen; } else if (ts.Minutes >= 10) { BackColor = Color.DarkOrange; } else if (ts.Minutes >= 1) { BackColor = Color.OrangeRed; } else { WarningAnimation(); } } else // UI for tray menu { this.Text = "Countdown - " + elapsedTime; // Decide which tray message to show if (ts.Days == 0 && ts.Hours == 2 && ts.Minutes == 0 && ts.Seconds == 00) { notifyIcon.BalloonTipText = "2 hours remaining until the power action will be executed."; notifyIcon.ShowBalloonTip(5000); } else if (ts.Days == 0 && ts.Hours == 1 && ts.Minutes == 0 && ts.Seconds == 00) { notifyIcon.BalloonTipText = "1 hour remaining until the power action will be executed."; notifyIcon.ShowBalloonTip(5000); } else if (ts.Days == 0 && ts.Hours == 0 && ts.Minutes == 30 && ts.Seconds == 00) { notifyIcon.BalloonTipText = "30 minutes remaining until the power action will be executed."; notifyIcon.ShowBalloonTip(5000); } else if (ts.Days == 0 && ts.Hours == 0 && ts.Minutes == 5 && ts.Seconds == 00) { notifyIcon.BalloonTipText = "5 minutes remaining until the power action will be executed."; notifyIcon.ShowBalloonTip(5000); } else if (ts.Days == 0 && ts.Hours == 0 && ts.Minutes == 0 && ts.Seconds == 30) { notifyIcon.BalloonTipText = "30 seconds remaining until the power action will be executed."; notifyIcon.ShowBalloonTip(5000); } } } // Correct window states if unexpected changes occur if (!UI && WindowState != FormWindowState.Minimized) { // Window is visible when UI is set to background operation ShowUI(); } else if (UI && WindowState == FormWindowState.Minimized) { // Window is hidden when UI is set to foreground operation HideUI(); } // Update UI Application.DoEvents(); }