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; namespace WinFormsLibrary1 { public partial class UserControlIntegerInput : UserControl { private event EventHandler? _elementChanged; private event Action? _errorOccured; public string Error { get; private set; } public int? InputtedInteger { get { if (checkBoxNullable.Checked) { return null; } if (textBoxInteger.Text.Equals(string.Empty) || textBoxInteger.Text == null) { throw new UncheckedNullException("Input was null, but checkbox wasnt checked"); } if (Int32.TryParse(textBoxInteger.Text, out var number)) { return number; } throw new UnexpectedTypeException("Input was non integer"); } set { textBoxInteger.Text = value.ToString(); checkBoxNullable.Checked = value == null; } } public event EventHandler ElementChanged { add { _elementChanged += value; } remove { _elementChanged -= value; } } public event Action AnErrorOccurred { add { _errorOccured += value; } remove { _errorOccured -= value; } } public UserControlIntegerInput() { InitializeComponent(); Error = string.Empty; } private void textBoxInteger_TextChanged(object sender, EventArgs e) { _elementChanged?.Invoke(this, e); } private void checkBoxNullable_CheckedChanged(object sender, EventArgs e) { textBoxInteger.Enabled = !checkBoxNullable.Checked; textBoxInteger.Text = null; _elementChanged?.Invoke(this, e); } } }