comparison ServerMonitor/Forms/SettingsForm.cs @ 8:052aa62cb42a

Single instance. Add autorun option. Add icons. General enhancements.
author Brad Greco <brad@bgreco.net>
date Sat, 09 Mar 2019 20:14:03 -0500
parents c1dffaac66fa
children 75ca86e0862c
comparison
equal deleted inserted replaced
7:8486ab7d2357 8:052aa62cb42a
1 using ServerMonitorApp.Properties; 1 using Microsoft.Win32;
2 using ServerMonitorApp.Properties;
2 using System; 3 using System;
3 using System.Collections.Generic; 4 using System.Collections.Generic;
4 using System.ComponentModel; 5 using System.ComponentModel;
5 using System.Data; 6 using System.Data;
6 using System.Drawing; 7 using System.Drawing;
12 13
13 namespace ServerMonitorApp 14 namespace ServerMonitorApp
14 { 15 {
15 public partial class SettingsForm : Form 16 public partial class SettingsForm : Form
16 { 17 {
18 private readonly string autorunKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
19 private readonly string autorunName = "ServerMonitor";
20
17 public SettingsForm() 21 public SettingsForm()
18 { 22 {
19 InitializeComponent(); 23 InitializeComponent();
20 } 24 }
21 25
22 private void SettingsForm_Load(object sender, EventArgs e) 26 private void SettingsForm_Load(object sender, EventArgs e)
23 { 27 {
28 Icon = Resources.icon;
24 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox }) 29 foreach (ComboBox comboBox in new object[] { ErrorComboBox, WarningComboBox, InformationComboBox })
25 { 30 {
26 comboBox.DataSource = Enum.GetValues(typeof(FailAction)); 31 comboBox.DataSource = Enum.GetValues(typeof(FailAction));
27 comboBox.Format += FailActionComboBox_Format; 32 comboBox.Format += FailActionComboBox_Format;
28 } 33 }
34 AutorunCheckBox.Checked = GetAutorun();
29 KeepLogDaysInput.Value = Settings.Default.KeepLogDays; 35 KeepLogDaysInput.Value = Settings.Default.KeepLogDays;
30 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction; 36 ErrorComboBox.SelectedItem = Settings.Default.ErrorAction;
31 WarningComboBox.SelectedItem = Settings.Default.WarningAction; 37 WarningComboBox.SelectedItem = Settings.Default.WarningAction;
32 InformationComboBox.SelectedItem = Settings.Default.InformationAction; 38 InformationComboBox.SelectedItem = Settings.Default.InformationAction;
39 }
40
41 private bool GetAutorun()
42 {
43 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, false);
44 string value = (string)key.GetValue(autorunName, string.Empty);
45 return value.StartsWith(Application.ExecutablePath);
46 }
47
48 private void SetAutorun(bool autorun)
49 {
50 RegistryKey key = Registry.CurrentUser.OpenSubKey(autorunKey, true);
51 if (autorun)
52 key.SetValue(autorunName, Application.ExecutablePath.ToString());
53 else
54 key.DeleteValue(autorunName, false);
33 } 55 }
34 56
35 private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e) 57 private void FailActionComboBox_Format(object sender, ListControlConvertEventArgs e)
36 { 58 {
37 e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1); 59 e.Value = e.Value.ToString().Substring(0, 1) + Regex.Replace(e.Value.ToString(), "(\\B[A-Z])", " $1").ToLower().Substring(1);
42 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value; 64 Settings.Default.KeepLogDays = (int)KeepLogDaysInput.Value;
43 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem; 65 Settings.Default.ErrorAction = (FailAction)ErrorComboBox.SelectedItem;
44 Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem; 66 Settings.Default.WarningAction = (FailAction)WarningComboBox.SelectedItem;
45 Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem; 67 Settings.Default.InformationAction = (FailAction)InformationComboBox.SelectedItem;
46 Settings.Default.Save(); 68 Settings.Default.Save();
69 SetAutorun(AutorunCheckBox.Checked);
47 Close(); 70 Close();
48 } 71 }
49 72
50 private void CancelSettingsButton_Click(object sender, EventArgs e) 73 private void CancelSettingsButton_Click(object sender, EventArgs e)
51 { 74 {