using System; using System.Drawing; using System.Windows.Forms; namespace DesktopTools.BaseControls { public partial class BaseControlInt : AbstractBaseControl { public BaseControlInt(string propertyName) : base(propertyName) { InitializeComponent(); checkBoxNullable.Visible = false; panelControl.Controls.Add(checkBoxNullable); checkBoxNullable.CheckedChanged += (object sender, EventArgs e) => { numericUpDown.Enabled = !(sender as CheckBox).Checked; CallOnValueChangeEvent(); }; panelControl.Controls.Add(numericUpDown); numericUpDown.ValueChanged += (object sender, EventArgs e) => { CallOnValueChangeEvent(); }; _originalValue = 0; } public override void SetReadOnly(bool readOnly) { numericUpDown.Enabled = !readOnly; checkBoxNullable.Enabled = !readOnly; } public override void SetValueToControl(object value) { base.SetValueToControl(value); if (value != null) { numericUpDown.Value = Convert.ToInt32(value); } } public override void DropValue() { numericUpDown.Value = _originalValue != null ? Convert.ToInt32(_originalValue) : 0; } public override bool CheckValue() { if (_mustCheckValue && checkBoxNullable.Checked) { BackColor = Color.OrangeRed; return false; } return true; } protected override object GetValueFromControl() { if (_mustCheckValue && checkBoxNullable.Checked) { throw new ArgumentNullException($"Поле свойства '{labelTitle.Text}' должно быть заполнено"); } return checkBoxNullable.Checked? null : Convert.ToInt32(numericUpDown.Value); } } }