using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Library15.Exceptions; namespace Library15 { public partial class InputRealNumber : UserControl { private event EventHandler? _valueChanged; public InputRealNumber() { InitializeComponent(); } public double? DoubleValue { get { if (IsNullCheckBox.Checked) { return null; } if (string.IsNullOrEmpty(MainTextBox.Text)) { throw new EmptyValueException(); } double ParsedDouble; if (double.TryParse(MainTextBox.Text, out ParsedDouble)) { return ParsedDouble; } else { throw new MalformedRealException(); } } set { SetNullState(value is null, true); if (value is not null) { MainTextBox.Text = value.ToString(); } } } public event EventHandler? ValueChanged { add { _valueChanged += value; } remove { _valueChanged -= value; } } private void SetNullState(bool IsNull, bool ClearText = false) { IsNullCheckBox.Checked = IsNull; MainTextBox.Enabled = !IsNull; if (ClearText) { MainTextBox.Text = string.Empty; } } private void IsNullCheckBox_CheckedChanged(object sender, EventArgs e) { SetNullState(IsNullCheckBox.Checked); _valueChanged?.Invoke(this, e); } private void MainTextBox_TextChanged(object sender, EventArgs e) { _valueChanged?.Invoke(this, e); } } }