comparison ServerMonitor/Forms/SettingsForm.cs @ 16:7626b099aefd

More comments.
author Brad Greco <brad@bgreco.net>
date Tue, 30 Apr 2019 20:40:58 -0400
parents 75ca86e0862c
children f6235dc0a8ec
comparison
equal deleted inserted replaced
15:23f2e0da1094 16:7626b099aefd
1 using Microsoft.Win32; 1 using Microsoft.Win32;
2 using ServerMonitorApp.Properties; 2 using ServerMonitorApp.Properties;
3 using System; 3 using System;
4 using System.Collections.Generic;
5 using System.ComponentModel;
6 using System.Data;
7 using System.Drawing;
8 using System.Linq;
9 using System.Text;
10 using System.Text.RegularExpressions; 4 using System.Text.RegularExpressions;
11 using System.Threading.Tasks;
12 using System.Windows.Forms; 5 using System.Windows.Forms;
13 6
14 namespace ServerMonitorApp 7 namespace ServerMonitorApp
15 { 8 {
9 /// <summary>Application settings form.</summary>
16 public partial class SettingsForm : Form 10 public partial class SettingsForm : Form
17 { 11 {
18 private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; 12 private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
19 private readonly string autorunName = "ServerMonitor"; 13 private readonly string autorunName = "ServerMonitor";
20 14
24 } 18 }
25 19
26 private void SettingsForm_Load(object sender, EventArgs e) 20 private void SettingsForm_Load(object sender, EventArgs e)
27 { 21 {
28 Icon = Resources.icon; 22 Icon = Resources.icon;
23 // Populate each action combo box with the available actions to perform on failure.
29 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox }) 24 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox })
30 { 25 {
31 comboBox.DataSource = Enum.GetValues(typeof(FailAction)); 26 comboBox.DataSource = Enum.GetValues(typeof(FailAction));
32 comboBox.Format += FailActionComboBox_Format; 27 comboBox.Format += FailActionComboBox_Format;
33 } 28 }
29 // Initialize controls with current settings.
34 AutorunCheckBox.Checked = GetAutorun(); 30 AutorunCheckBox.Checked = GetAutorun();
35 KeepRunningCheckBox.Checked = Settings.Default.HideToNotificationArea; 31 KeepRunningCheckBox.Checked = Settings.Default.HideToNotificationArea;
36 KeepLogDaysInput.Value = Settings.Default.KeepLogDays; 32 KeepLogDaysInput.Value = Settings.Default.KeepLogDays;
37 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction; 33 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction;
38 WarningComboBox.SelectedItem = Settings.Default.WarningAction; 34 WarningComboBox.SelectedItem = Settings.Default.WarningAction;
39 InformationComboBox.SelectedItem = Settings.Default.InformationAction; 35 InformationComboBox.SelectedItem = Settings.Default.InformationAction;
40 } 36 }
41 37
38 /// <summary>Gets whether an autorun registry key for this program exists.</summary>
39 /// <returns>Whether an autorun registry key for this program exists</returns>
42 private bool GetAutorun() 40 private bool GetAutorun()
43 { 41 {
44 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false); 42 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false);
45 string value = (string)key.GetValue(autorunName, string.Empty); 43 string value = (string)key.GetValue(autorunName, string.Empty);
46 return value.StartsWith(Application.ExecutablePath); 44 return value.StartsWith(Application.ExecutablePath);
47 } 45 }
48 46
47 /// <summary>Sets whether this program should automatically start with Windows.</summary>
48 /// <param name="autorun">Whether autorun should be enabled or disabled.</param>
49 private void SetAutorun(bool autorun) 49 private void SetAutorun(bool autorun)
50 { 50 {
51 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, true); 51 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, true);
52 if (autorun) 52 if (autorun)
53 key.SetValue(autorunName, Application.ExecutablePath.ToString()); 53 key.SetValue(autorunName, Application.ExecutablePath.ToString());
54 else 54 else
55 key.DeleteValue(autorunName, false); 55 key.DeleteValue(autorunName, false);
56 } 56 }
57 57
58 /// <summary>Shows a human readable description of possible check failure actions in the combo box.</summary>
58 private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e) 59 private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e)
59 { 60 {
61 // Transform the "CamelCase" enum name in to a "Camel case" name, adding
62 // spaces and making all characters besides the first lower case.
60 e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1); 63 e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1);
61 } 64 }
62 65
66 /// <summary>Saves the user settings and closes the form.</summary>
63 private void OkButton_Click(object sender, EventArgs e) 67 private void OkButton_Click(object sender, EventArgs e)
64 { 68 {
65 Settings.Default.HideToNotificationArea = KeepRunningCheckBox.Checked; 69 Settings.Default.HideToNotificationArea = KeepRunningCheckBox.Checked;
66 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value; 70 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value;
67 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem; 71 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem;
70 Settings.Default.Save(); 74 Settings.Default.Save();
71 SetAutorun(AutorunCheckBox.Checked); 75 SetAutorun(AutorunCheckBox.Checked);
72 Close(); 76 Close();
73 } 77 }
74 78
79 /// <summary>Closes the form without saving settings.</summary>
75 private void CancelSettingsButton_Click(object sender, EventArgs e) 80 private void CancelSettingsButton_Click(object sender, EventArgs e)
76 { 81 {
77 Close(); 82 Close();
78 } 83 }
79 } 84 }