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 System; using System.Windows.Forms; using FormLibrary.Exceptions; namespace FormLibrary { public partial class IntegerInputControl : UserControl { public event EventHandler? ValueChanged; public event EventHandler? CheckBoxChanged; public IntegerInputControl() { InitializeComponent(); checkBoxNull.CheckedChanged += CheckBoxNull_CheckedChanged; textBoxInput.TextChanged += TextBoxInput_TextChanged; } public int? Value { get { if (checkBoxNull.Checked) { return null; } else { if (string.IsNullOrWhiteSpace(textBoxInput.Text)) { throw new EmptyValueException(); } if (int.TryParse(textBoxInput.Text, out int result)) { return result; } else { throw new InvalidValueTypeException(); } } } set { if(value is not null) { textBoxInput.Text = value.ToString(); } checkBoxNull.Checked = value is null; } } private void CheckBoxNull_CheckedChanged(object sender, EventArgs e) { textBoxInput.Enabled = !checkBoxNull.Checked; if (checkBoxNull.Checked) { textBoxInput.Text = string.Empty; } CheckBoxChanged?.Invoke(this, EventArgs.Empty); } private void TextBoxInput_TextChanged(object sender, EventArgs e) { ValueChanged?.Invoke(this, EventArgs.Empty); } } }