using ToolsDesktop.Interfaces; using System; using System.Windows.Forms; namespace ToolsDesktop.BaseControls { /// /// Контрол, предоставляющий работу с вещественным полем /// public partial class BaseControlDecimal : AbstractBaseControl, IBaseControl { /// /// Конструктор /// /// /// /// /// /// /// public BaseControlDecimal(string propertyName, bool mustFilling, bool readOnly, double minValue, double maxValue, int decimalPlaces) : 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; } if (decimalPlaces != 0) { numericUpDown.DecimalPlaces = decimalPlaces; } 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.ToDecimal(value); } else if (!_mustFilling) { checkBoxNullable.Checked = true; } } public void DropValueForControl() { if (_originalValue != null) { numericUpDown.Value = Convert.ToDecimal(_originalValue); } else if (!_mustFilling) { checkBoxNullable.Checked = true; } } public bool CheckValueForControl() => true; public object GetValueFromControl() => !_mustFilling && checkBoxNullable.Checked ? null : Convert.ToDecimal(numericUpDown.Value); public string GetPropertyName() => _propertyName; } }