using DesktopTools.Interfaces; using System; using System.Windows.Forms; namespace DesktopTools.BaseControls { /// /// Контрол, предоставляющий работу с целочисленным полем /// public partial class BaseControlInt : AbstractBaseControl, IBaseControl { /// /// Конструктор /// /// /// /// /// /// public BaseControlInt(string propertyName, bool mustFilling, bool readOnly, double minValue, double maxValue) : base(propertyName, mustFilling, readOnly) { InitializeComponent(); _baseControl = this; numericUpDown.ValueChanged += (object sender, EventArgs e) => { CallOnValueChangeEvent(); }; if (minValue != 0) { numericUpDown.Minimum = (decimal)minValue; } if (maxValue != 0) { numericUpDown.Maximum = (decimal)maxValue; } panelControl.Controls.Add(numericUpDown); if (!_mustFilling) { checkBoxNullable.CheckedChanged += (object sender, EventArgs e) => { numericUpDown.Enabled = !(sender as CheckBox).Checked; CallOnValueChangeEvent(); }; panelControl.Controls.Add(checkBoxNullable); } } public void SetDefaultValue() => _originalValue = _mustFilling ? 0 : null; public void SetValueToControl(object value) { if (value != null) { numericUpDown.Value = Convert.ToInt32(value); } else if (!_mustFilling) { checkBoxNullable.Checked = true; } } public void DropValueForControl() { if (_originalValue != null) { numericUpDown.Value = Convert.ToInt32(_originalValue); } else if (!_mustFilling) { checkBoxNullable.Checked = true; } } public bool CheckValueForControl() => true; public object GetValueFromControl() => !_mustFilling && checkBoxNullable.Checked ? null : Convert.ToInt32(numericUpDown.Value); public string GetPropertyName() => _propertyName; } }