From 47a5e5c7265f51bce3e4d22f5505f085e03ce9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Thu, 5 Sep 2024 23:14:57 +0400 Subject: [PATCH] Create ComboBox --- .../ComponentProgramming/ControlComboBox.cs | 25 ++-- .../ControlTextBox.Designer.cs | 70 ++++++++++ .../ComponentProgramming/ControlTextBox.cs | 89 +++++++++++++ .../ComponentProgramming/ControlTextBox.resx | 120 ++++++++++++++++++ .../Exceptions/NumberException.cs | 18 +++ ComponentProgramming/Forms/Form.Designer.cs | 12 ++ ComponentProgramming/Forms/Form.cs | 23 +++- 7 files changed, 343 insertions(+), 14 deletions(-) create mode 100644 ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs create mode 100644 ComponentProgramming/ComponentProgramming/ControlTextBox.cs create mode 100644 ComponentProgramming/ComponentProgramming/ControlTextBox.resx create mode 100644 ComponentProgramming/ComponentProgramming/Exceptions/NumberException.cs diff --git a/ComponentProgramming/ComponentProgramming/ControlComboBox.cs b/ComponentProgramming/ComponentProgramming/ControlComboBox.cs index 8766ef9..48b8905 100644 --- a/ComponentProgramming/ComponentProgramming/ControlComboBox.cs +++ b/ComponentProgramming/ComponentProgramming/ControlComboBox.cs @@ -8,19 +8,28 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static System.Runtime.InteropServices.JavaScript.JSType; +using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace ComponentProgramming { public partial class ControlComboBox : UserControl { private event EventHandler? _comboBoxChanged; - private event Action? _errorOccured; - public string Error { get; private set; } - public object?[] elements + public string elements { - get { return comboBoxElements.SelectedItem == null ? [" "] : [comboBoxElements.SelectedItem]; } - set { comboBoxElements.Items.AddRange(value); } + get + { + if (comboBoxElements.SelectedItem == null) + { + return ""; + } + return comboBoxElements.SelectedItem.ToString()!; + } + set + { + comboBoxElements.Items.AddRange(value.Split(", ")); + } } public event EventHandler ComboBoxChanged @@ -28,16 +37,10 @@ namespace ComponentProgramming add { _comboBoxChanged += value; } remove { _comboBoxChanged -= value; } } - public event Action AnErrorOccured - { - add { _errorOccured += value; } - remove { _errorOccured -= value; } - } public ControlComboBox() { InitializeComponent(); - Error = string.Empty; } public void ClearComboBox() diff --git a/ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs b/ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs new file mode 100644 index 0000000..0c7b663 --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs @@ -0,0 +1,70 @@ +namespace ComponentProgramming +{ + partial class ControlTextBox + { + /// + /// Обязательная переменная конструктора. + /// + 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() + { + textBox = new TextBox(); + checkBox = new CheckBox(); + SuspendLayout(); + // + // textBox + // + textBox.Location = new Point(30, 3); + textBox.Name = "textBox"; + textBox.Size = new Size(117, 23); + textBox.TabIndex = 0; + textBox.KeyPress += textBox_KeyPress; + // + // checkBox + // + checkBox.AutoSize = true; + checkBox.Location = new Point(9, 7); + checkBox.Name = "checkBox"; + checkBox.Size = new Size(15, 14); + checkBox.TabIndex = 1; + checkBox.UseVisualStyleBackColor = true; + checkBox.CheckedChanged += checkBox_CheckedChanged; + // + // ControlTextBox + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(checkBox); + Controls.Add(textBox); + Name = "ControlTextBox"; + Size = new Size(150, 29); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox textBox; + private CheckBox checkBox; + } +} diff --git a/ComponentProgramming/ComponentProgramming/ControlTextBox.cs b/ComponentProgramming/ComponentProgramming/ControlTextBox.cs new file mode 100644 index 0000000..5283f63 --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/ControlTextBox.cs @@ -0,0 +1,89 @@ +using ComponentProgramming.Exceptions; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ComponentProgramming +{ + public partial class ControlTextBox : UserControl + { + + + private Regex regex = new(@"^\+7\d{10}$"); + + public string? text + { + get + { + if (checkBox.Checked) + { + return textBox.Text = null; + } + if (regex.IsMatch(textBox.Text)) + { + return textBox.Text; + } + else + { + throw new NumberException(textBox.Text + " не соответствует стандарту!"); + } + } + set + { + if (checkBox.Checked) + { + textBox.Text = null; + } + if (regex.IsMatch(value!)) + { + textBox.Text = value; + } + else + { + throw new NumberException(textBox.Text + " не соответствует стандарту!"); + } + } + } + + public ControlTextBox() + { + InitializeComponent(); + } + + private event EventHandler? _checkBoxChanged; + + public event EventHandler CheckBoxChanged + { + add { _checkBoxChanged += value; } + remove { _checkBoxChanged -= value; } + } + + private void checkBox_CheckedChanged(object sender, EventArgs e) + { + if (checkBox.Checked) + { + textBox.Enabled = false; + _checkBoxChanged?.Invoke(this, e); + } + else + { + textBox.Enabled = true; + } + } + + private void textBox_KeyPress(object sender, KeyPressEventArgs e) + { + if(e.KeyChar == '\r') + { + text = textBox.Text; + } + } + } +} diff --git a/ComponentProgramming/ComponentProgramming/ControlTextBox.resx b/ComponentProgramming/ComponentProgramming/ControlTextBox.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/ControlTextBox.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/ComponentProgramming/ComponentProgramming/Exceptions/NumberException.cs b/ComponentProgramming/ComponentProgramming/Exceptions/NumberException.cs new file mode 100644 index 0000000..fed8d3c --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/Exceptions/NumberException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComponentProgramming.Exceptions +{ + public class NumberException : Exception + { + public NumberException() { } + + public NumberException(string message) : base(message) + { + + } + } +} diff --git a/ComponentProgramming/Forms/Form.Designer.cs b/ComponentProgramming/Forms/Form.Designer.cs index d715ba8..363c7d0 100644 --- a/ComponentProgramming/Forms/Form.Designer.cs +++ b/ComponentProgramming/Forms/Form.Designer.cs @@ -29,21 +29,32 @@ private void InitializeComponent() { controlComboBox = new ComponentProgramming.ControlComboBox(); + controlTextBox = new ComponentProgramming.ControlTextBox(); SuspendLayout(); // // controlComboBox // + controlComboBox.elements = ""; controlComboBox.Location = new Point(12, 3); controlComboBox.Name = "controlComboBox"; controlComboBox.Size = new Size(177, 31); controlComboBox.TabIndex = 0; controlComboBox.ComboBoxChanged += controlComboBox_ComboBoxChanged; // + // controlTextBox + // + controlTextBox.Location = new Point(226, 3); + controlTextBox.Name = "controlTextBox"; + controlTextBox.Size = new Size(150, 29); + controlTextBox.TabIndex = 1; + controlTextBox.CheckBoxChanged += controlTextBox_CheckBoxChanged; + // // Form // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(controlTextBox); Controls.Add(controlComboBox); Name = "Form"; Text = "Form"; @@ -54,5 +65,6 @@ private ComponentProgramming.ControlImage control; private ComponentProgramming.ControlComboBox controlComboBox; + private ComponentProgramming.ControlTextBox controlTextBox; } } diff --git a/ComponentProgramming/Forms/Form.cs b/ComponentProgramming/Forms/Form.cs index 0c9924b..7b5cea9 100644 --- a/ComponentProgramming/Forms/Form.cs +++ b/ComponentProgramming/Forms/Form.cs @@ -6,17 +6,34 @@ namespace Forms { InitializeComponent(); FillBox(); + FillTextBox(); } private void FillBox() { - controlComboBox.elements = new object[] {" 1", " 2", " 3", " 4" }; + controlComboBox.elements = " 1, 2, 3, 4"; + } + private void FillTextBox() + { + controlTextBox.text = "+79063908075"; } private void controlComboBox_ComboBoxChanged(object sender, EventArgs e) { - var elem = controlComboBox.elements[0]; - MessageBox.Show($": {elem?.ToString()}"); + var elem = controlComboBox.elements; + MessageBox.Show($": {elem}"); + } + + private void controlTextBox_CheckBoxChanged(object sender, EventArgs e) + { + if (controlTextBox.text == null) + { + MessageBox.Show($"CheckBox checked"); + } + else + { + MessageBox.Show($"CheckBox not checked"); + } } } }