comparison ServerMonitor/Controls/CheckControl.cs @ 0:3e1a2131f897

Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
author Brad Greco <brad@bgreco.net>
date Mon, 31 Dec 2018 18:32:14 -0500
parents
children 453ecc1ed9ea
comparison
equal deleted inserted replaced
-1:000000000000 0:3e1a2131f897
1 ´╗┐using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace ServerMonitorApp
11 {
12 /// <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>
14 public partial class CheckControl : UserControl
15 {
16 public Type CheckType { get { return GetCheckType(GetType()); } }
17
18 public CheckControl()
19 {
20 InitializeComponent();
21 }
22
23 private void CheckControl_Load(object sender, EventArgs e)
24 {
25 CheckGroupBox.Text = Helpers.GetDisplayName(CheckType);
26
27 IEnumerable<Panel> panels = CheckGroupBox.Controls.OfType<Panel>();
28 foreach (Panel panel in panels)
29 {
30 CheckBox mainCheckBox = panel.Controls.OfType<CheckBox>().OrderBy(c => c.Left).First();
31 mainCheckBox.CheckedChanged += CheckControl_CheckedChanged;
32 DisablePanelByCheckBox(mainCheckBox);
33 }
34 }
35
36 private void CheckControl_CheckedChanged(object sender, EventArgs e)
37 {
38 DisablePanelByCheckBox((CheckBox)sender);
39 }
40
41 private void DisablePanelByCheckBox(CheckBox checkBox)
42 {
43 foreach (Control control in checkBox.Parent.Controls)
44 {
45 if (control != checkBox)
46 control.Enabled = checkBox.Checked;
47 }
48 }
49
50 public virtual void LoadCheck(Check check) { }
51
52 public virtual void UpdateCheck(Check check) { }
53
54 public static CheckControl Create(Type CheckType)
55 {
56 Type checkControlType = typeof(CheckControl).Assembly.GetTypes().FirstOrDefault(t => GetCheckType(t) == CheckType);
57 return checkControlType == null ? null : (CheckControl)Activator.CreateInstance(checkControlType);
58 }
59
60 private static Type GetCheckType(Type type)
61 {
62 return type.GetAttribute<CheckTypeAttribute>()?.CheckType;
63 }
64 }
65 }