diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.Designer.cs b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.Designer.cs new file mode 100644 index 0000000..5ca8ae1 --- /dev/null +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.Designer.cs @@ -0,0 +1,71 @@ +namespace Cop.Borovkov.Var3.Components +{ + partial class CustomNumericInputField + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + isNullcheckBox = new CheckBox(); + inputField = new TextBox(); + SuspendLayout(); + // + // isNullcheckBox + // + isNullcheckBox.AutoSize = true; + isNullcheckBox.Location = new Point(3, 9); + isNullcheckBox.Name = "isNullcheckBox"; + isNullcheckBox.Size = new Size(18, 17); + isNullcheckBox.TabIndex = 0; + isNullcheckBox.UseVisualStyleBackColor = true; + isNullcheckBox.CheckedChanged += IsNullcheckBox_CheckedChanged; + // + // inputField + // + inputField.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + inputField.Location = new Point(27, 3); + inputField.Name = "inputField"; + inputField.Size = new Size(197, 27); + inputField.TabIndex = 1; + inputField.DataContextChanged += InputField_DataContextChanged; + // + // CustomNumericInputField + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(inputField); + Controls.Add(isNullcheckBox); + Name = "CustomNumericInputField"; + Size = new Size(227, 34); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private CheckBox isNullcheckBox; + private TextBox inputField; + } +} diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.cs b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.cs new file mode 100644 index 0000000..e239e30 --- /dev/null +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.cs @@ -0,0 +1,101 @@ +namespace Cop.Borovkov.Var3.Components +{ + public partial class CustomNumericInputField : UserControl + { + private event EventHandler? _numericInputChanged; + + private event Action? _errorOccured; + + /// + /// + public CustomNumericInputField() + { + InitializeComponent(); + } + + /// + /// Событие возникающие при изменении поля ввода + /// + public event EventHandler NumericInputChanged + { + add => _numericInputChanged += value; + remove => _numericInputChanged -= value; + } + + /// + /// Обработка ошибок + /// + public event Action AnErrorOccurred + { + add => _errorOccured += value; + remove => _errorOccured -= value; + } + + /// + /// Значения поля + /// + public int? Value + { + get + { + if (isNullcheckBox.Checked) + { + return null; + } + + if (int.TryParse(inputField.Text, out var value)) + { + return value; + } + + throw new InvalidOperationException(); + } + set + { + if (value is null) + { + isNullcheckBox.Checked = true; + } + else + { + isNullcheckBox.Checked = false; + inputField.Text = value.ToString(); + } + } + } + + private void IsNullcheckBox_CheckedChanged(object sender, EventArgs e) + { + if (isNullcheckBox.Checked) + { + inputField.Text = string.Empty; + inputField.Enabled = false; + } + else + { + inputField.Enabled = true; + } + + try + { + _numericInputChanged?.Invoke(sender, e); + } + catch (Exception ex) + { + _errorOccured?.Invoke(ex); + } + } + + private void InputField_DataContextChanged(object sender, EventArgs e) + { + try + { + _numericInputChanged?.Invoke(sender, e); + } + catch (Exception ex) + { + _errorOccured?.Invoke(ex); + } + } + } +} diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.resx b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Components/CustomNumericInputField.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Cop.Borovkov.Var3.csproj b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Cop.Borovkov.Var3.csproj index 3e210aa..cd9d366 100644 --- a/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Cop.Borovkov.Var3.csproj +++ b/Cop.Borovkov.Var3/Cop.Borovkov.Var3/Cop.Borovkov.Var3.csproj @@ -7,4 +7,8 @@ enable + + + +