Mercurial > servermonitor
comparison ServerMonitor/Controls/CheckControl.cs @ 9:7127d5b5ac75
Code cleanup and comments
author | Brad Greco <brad@bgreco.net> |
---|---|
date | Mon, 08 Apr 2019 21:29:54 -0400 |
parents | 3142e52cbe69 |
children |
comparison
equal
deleted
inserted
replaced
8:052aa62cb42a | 9:7127d5b5ac75 |
---|---|
1 using System; | 1 using System; |
2 using System.Collections.Generic; | 2 using System.Collections.Generic; |
3 using System.ComponentModel; | |
4 using System.Drawing; | |
5 using System.Data; | 3 using System.Data; |
6 using System.Linq; | 4 using System.Linq; |
7 using System.Text; | |
8 using System.Windows.Forms; | 5 using System.Windows.Forms; |
9 | 6 |
10 namespace ServerMonitorApp | 7 namespace ServerMonitorApp |
11 { | 8 { |
12 /// <summary>Base class for check controls</summary> | 9 /// <summary>Base class for check controls.</summary> |
13 /// <remarks>This control should never be used directly, but marking it abstract causes problems with the designer.</remarks> | 10 /// <remarks>This control should never be used directly, but marking it abstract causes problems with the designer.</remarks> |
14 public partial class CheckControl : UserControl | 11 public partial class CheckControl : UserControl |
15 { | 12 { |
13 /// <summary>Type of the check this control can edit.</summary> | |
16 public Type CheckType => GetCheckType(GetType()); | 14 public Type CheckType => GetCheckType(GetType()); |
17 | 15 |
18 public CheckControl() | 16 public CheckControl() |
19 { | 17 { |
20 InitializeComponent(); | 18 InitializeComponent(); |
22 | 20 |
23 private void CheckControl_Load(object sender, EventArgs e) | 21 private void CheckControl_Load(object sender, EventArgs e) |
24 { | 22 { |
25 CheckGroupBox.Text = Helpers.GetDisplayName(CheckType); | 23 CheckGroupBox.Text = Helpers.GetDisplayName(CheckType); |
26 | 24 |
25 // Bind change listeners to checkboxes to enable or disable a panel of controls. | |
26 // The leftmost checkbox is assumed to be the "master switch" for the panel. | |
27 IEnumerable<Panel> panels = CheckGroupBox.Controls.OfType<Panel>(); | 27 IEnumerable<Panel> panels = CheckGroupBox.Controls.OfType<Panel>(); |
28 foreach (Panel panel in panels) | 28 foreach (Panel panel in panels) |
29 { | 29 { |
30 CheckBox mainCheckBox = panel.Controls.OfType<CheckBox>().OrderBy(c => c.Left).FirstOrDefault(); | 30 CheckBox mainCheckBox = panel.Controls.OfType<CheckBox>().OrderBy(c => c.Left).FirstOrDefault(); |
31 if (mainCheckBox != null) | 31 if (mainCheckBox != null) |
39 private void CheckControl_CheckedChanged(object sender, EventArgs e) | 39 private void CheckControl_CheckedChanged(object sender, EventArgs e) |
40 { | 40 { |
41 DisablePanelByCheckBox((CheckBox)sender); | 41 DisablePanelByCheckBox((CheckBox)sender); |
42 } | 42 } |
43 | 43 |
44 /// <summary>Enables or disables all sibling controls of a checkbox.</summary> | |
45 /// <param name="checkBox">The checkbox to disable or enable the sibling controls of.</param> | |
44 private void DisablePanelByCheckBox(CheckBox checkBox) | 46 private void DisablePanelByCheckBox(CheckBox checkBox) |
45 { | 47 { |
46 foreach (Control control in checkBox.Parent.Controls) | 48 foreach (Control control in checkBox.Parent.Controls) |
47 { | 49 { |
48 if (control != checkBox) | 50 if (control != checkBox) |
49 control.Enabled = checkBox.Checked; | 51 control.Enabled = checkBox.Checked; |
50 } | 52 } |
51 } | 53 } |
52 | 54 |
55 /// <summary>Sets the values of the controls from a check's properties.</summary> | |
56 /// <remarks>Subclasses should implement this method. Marking as abstract causes problems with the designer.</remarks> | |
53 public virtual void LoadCheck(Check check) { } | 57 public virtual void LoadCheck(Check check) { } |
54 | 58 |
59 /// <summary>Updates the properties of a check from user input.</summary> | |
60 /// <remarks>Subclasses should implement this method. Marking as abstract causes problems with the designer.</remarks> | |
55 public virtual void UpdateCheck(Check check) { } | 61 public virtual void UpdateCheck(Check check) { } |
56 | 62 |
63 /// <summary>Creates an instance of a check control based on a check type.</summary> | |
57 public static CheckControl Create(Type CheckType) | 64 public static CheckControl Create(Type CheckType) |
58 { | 65 { |
66 // Use reflection to find a check control that declares it handles the given type. | |
59 Type checkControlType = typeof(CheckControl).Assembly.GetTypes().FirstOrDefault(t => GetCheckType(t) == CheckType); | 67 Type checkControlType = typeof(CheckControl).Assembly.GetTypes().FirstOrDefault(t => GetCheckType(t) == CheckType); |
68 // If one was found, instantiate it. | |
60 return checkControlType == null ? null : (CheckControl)Activator.CreateInstance(checkControlType); | 69 return checkControlType == null ? null : (CheckControl)Activator.CreateInstance(checkControlType); |
61 } | 70 } |
62 | 71 |
72 /// <summary>Gets the value of the CheckTypeAttribute for a type.</summary> | |
63 private static Type GetCheckType(Type type) | 73 private static Type GetCheckType(Type type) |
64 { | 74 { |
65 return type.GetAttribute<CheckTypeAttribute>()?.CheckType; | 75 return type.GetAttribute<CheckTypeAttribute>()?.CheckType; |
66 } | 76 } |
67 } | 77 } |