annotate ServerMonitor/Forms/SettingsForm.cs @ 9:7127d5b5ac75

Code cleanup and comments
author Brad Greco <brad@bgreco.net>
date Mon, 08 Apr 2019 21:29:54 -0400
parents 052aa62cb42a
children 75ca86e0862c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
1 using Microsoft.Win32;
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
2 using ServerMonitorApp.Properties;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
3 using System;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
4 using System.Collections.Generic;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
5 using System.ComponentModel;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
6 using System.Data;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
7 using System.Drawing;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
8 using System.Linq;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
9 using System.Text;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
10 using System.Text.RegularExpressions;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
11 using System.Threading.Tasks;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
12 using System.Windows.Forms;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
13
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
14 namespace ServerMonitorApp
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
15 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
16 public partial class SettingsForm : Form
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
17 {
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
18 private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
19 private readonly string autorunName = "ServerMonitor";
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
20
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
21 public SettingsForm()
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
22 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
23 InitializeComponent();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
24 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
25
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
26 private void SettingsForm_Load(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
27 {
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
28 Icon = Resources.icon;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
29 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox })
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
30 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
31 comboBox.DataSource = Enum.GetValues(typeof(FailAction));
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
32 comboBox.Format += FailActionComboBox_Format;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
33 }
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
34 AutorunCheckBox.Checked = GetAutorun();
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
35 KeepLogDaysInput.Value = Settings.Default.KeepLogDays;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
36 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
37 WarningComboBox.SelectedItem = Settings.Default.WarningAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
38 InformationComboBox.SelectedItem = Settings.Default.InformationAction;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
39 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
40
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
41 private bool GetAutorun()
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
42 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
43 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
44 string value = (string)key.GetValue(autorunName, string.Empty);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
45 return value.StartsWith(Application.ExecutablePath);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
46 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
47
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
48 private void SetAutorun(bool autorun)
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
49 {
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
50 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, true);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
51 if (autorun)
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
52 key.SetValue(autorunName, Application.ExecutablePath.ToString());
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
53 else
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
54 key.DeleteValue(autorunName, false);
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
55 }
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
56
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
57 private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
58 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
59 e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1);
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
60 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
61
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
62 private void OkButton_Click(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
63 {
6
c1dffaac66fa - Don't show multiple password dialogs for the same key if the first one was cancelled.
Brad Greco <brad@bgreco.net>
parents: 4
diff changeset
64 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value;
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
65 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
66 Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
67 Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem;
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
68 Settings.Default.Save();
8
052aa62cb42a Single instance. Add autorun option. Add icons. General enhancements.
Brad Greco <brad@bgreco.net>
parents: 6
diff changeset
69 SetAutorun(AutorunCheckBox.Checked);
4
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
70 Close();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
71 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
72
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
73 private void CancelSettingsButton_Click(object sender, EventArgs e)
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
74 {
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
75 Close();
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
76 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
77 }
3142e52cbe69 Lots more progress
Brad Greco <brad@bgreco.net>
parents:
diff changeset
78 }