view ServerMonitor/Controls/DiskSpaceCheckControl.cs @ 23:3866c19535fd

Fix NullReferenceException when checks are executed on a brand new server.
author Brad Greco <brad@bgreco.net>
date Thu, 30 May 2019 21:40:27 -0400
parents 7127d5b5ac75
children
line wrap: on
line source

using System;

namespace ServerMonitorApp
{
    /// <summary>Control for editing a disk space check.</summary>
    [CheckType(typeof(DiskSpaceCheck))]
    public partial class DiskSpaceCheckControl : CheckControl
    {
        public DiskSpaceCheckControl()
        {
            InitializeComponent();
        }

        private void DiskSpaceCheckControl_Load(object sender, EventArgs e)
        {
            // Initialize the free space units to a non-empty value.
            FreeSpaceUnitsComboBox.SelectedIndex = 1;
        }

        /// <summary>Sets the values of the controls from a check's properties.</summary>
        public override void LoadCheck(Check check1)
        {
            DiskSpaceCheck check = (DiskSpaceCheck)check1;
            DeviceTextBox.Text = check.Device;
            FreeSpaceTextBox.Text = check.MinFreeSpace.ToString();
            FreeSpaceUnitsComboBox.SelectedIndex = (int)check.FreeSpaceUnits;
        }

        /// <summary>Updates the properties of a check from user input.</summary>
        public override void UpdateCheck(Check check1)
        {
            DiskSpaceCheck check = (DiskSpaceCheck)check1;
            check.Device = DeviceTextBox.Text.Trim();
            check.FreeSpaceUnits = (FreeSpaceUnits)FreeSpaceUnitsComboBox.SelectedIndex;
            try
            {
                check.MinFreeSpace = double.Parse(FreeSpaceTextBox.Text);
            }
            catch
            {
                check.MinFreeSpace = 0;
                throw new UpdateCheckException("Free space must be numeric.");
            }
        }
    }
}