From c4bb4272c6b797494dad72b68c5588fdbb5e3e70 Mon Sep 17 00:00:00 2001 From: kamilia Date: Mon, 16 Sep 2024 21:57:13 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D1=83=20=D1=87=D0=B5=20=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BF=D0=BE=D1=88=D0=B0=D0=BC=D0=B0=D0=BD=D0=B8=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- COP_7/DropDownList.Designer.cs | 2 +- COP_7/DropDownList.cs | 71 ++++---- COP_7/Exceptions/PhoneException.cs | 20 +++ COP_7/ListBoxMany.Designer.cs | 35 ++-- COP_7/ListBoxMany.cs | 159 ++++++++++-------- COP_7/ListBoxMany.resx | 62 ++++++- COP_7/PhoneTextBox.cs | 100 ----------- COP_7/PhoneTextBox.resx | 60 ------- ...x.Designer.cs => TextBoxPhone.Designer.cs} | 43 ++--- COP_7/TextBoxPhone.cs | 111 ++++++++++++ COP_7/TextBoxPhone.resx | 123 ++++++++++++++ FormTest/Form1.Designer.cs | 43 +++-- FormTest/Form1.cs | 10 ++ 13 files changed, 528 insertions(+), 311 deletions(-) create mode 100644 COP_7/Exceptions/PhoneException.cs delete mode 100644 COP_7/PhoneTextBox.cs delete mode 100644 COP_7/PhoneTextBox.resx rename COP_7/{PhoneTextBox.Designer.cs => TextBoxPhone.Designer.cs} (57%) create mode 100644 COP_7/TextBoxPhone.cs create mode 100644 COP_7/TextBoxPhone.resx diff --git a/COP_7/DropDownList.Designer.cs b/COP_7/DropDownList.Designer.cs index 5247332..5d87be2 100644 --- a/COP_7/DropDownList.Designer.cs +++ b/COP_7/DropDownList.Designer.cs @@ -1,4 +1,4 @@ -namespace COP_7 +namespace Components { partial class DropDownList { diff --git a/COP_7/DropDownList.cs b/COP_7/DropDownList.cs index 4a3a369..402d7d5 100644 --- a/COP_7/DropDownList.cs +++ b/COP_7/DropDownList.cs @@ -8,39 +8,28 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace COP_7 +namespace Components { public partial class DropDownList : UserControl { - - public ComboBox.ObjectCollection Items => ComboBox.Items; - - public event EventHandler _сhangeEvent; - - public event EventHandler ChangeEvent - { - add - { - _сhangeEvent += value; - } - remove - { - _сhangeEvent -= value; - } - - } - public DropDownList() { InitializeComponent(); - } - public string SelectedValue + public string SelectedItem { get { - return ComboBox.Items.Contains(ComboBox.SelectedItem) ? ComboBox.SelectedItem?.ToString() ?? "" : ""; + if (ComboBox.Items.Count == 0) + { + return " "; + } + if (ComboBox.SelectedItem == null) + { + return " "; + } + return ComboBox.SelectedItem.ToString()!; } set { @@ -48,21 +37,41 @@ namespace COP_7 { ComboBox.SelectedItem = value; } - else - { - ComboBox.SelectedIndex = -1; - } + } } - public void Clear() + + public ComboBox.ObjectCollection ComboBoxItems { - Items.Clear(); + get { return ComboBox.Items; } } - private void comboBox_SelectedIndexChanged(object sender, EventArgs e) + + private EventHandler _onValueChangedEvent; + + public event EventHandler ValueChanged { - _сhangeEvent?.Invoke(this, e); + add + { + _onValueChangedEvent += value; + } + + remove + { + _onValueChangedEvent -= value; + } + } + + private void CustomComboBox_SelectedValueChanged(object sender, EventArgs e) + { + _onValueChangedEvent?.Invoke(sender, e); + } + + public void ComboBoxClear() + { + ComboBox.Items.Clear(); + ComboBox.SelectedItem = null; } } -} +} \ No newline at end of file diff --git a/COP_7/Exceptions/PhoneException.cs b/COP_7/Exceptions/PhoneException.cs new file mode 100644 index 0000000..2165ffa --- /dev/null +++ b/COP_7/Exceptions/PhoneException.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Components.Exceptions +{ + public class PhoneException : Exception + { + public PhoneException() { } + + /// + /// Конструктор с сообщением ошибки + /// + public PhoneException(string message) : base(message) + { + } + } +} diff --git a/COP_7/ListBoxMany.Designer.cs b/COP_7/ListBoxMany.Designer.cs index da76708..76b0b11 100644 --- a/COP_7/ListBoxMany.Designer.cs +++ b/COP_7/ListBoxMany.Designer.cs @@ -1,4 +1,4 @@ -namespace CustomComponent +namespace Components { partial class ListBoxMany { @@ -28,30 +28,27 @@ /// private void InitializeComponent() { - this.listBox = new System.Windows.Forms.ListBox(); - this.SuspendLayout(); + listBox = new ListBox(); + SuspendLayout(); // // listBox // - this.listBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - this.listBox.FormattingEnabled = true; - this.listBox.ItemHeight = 15; - this.listBox.Location = new System.Drawing.Point(0, 0); - this.listBox.Name = "listBox"; - this.listBox.Size = new System.Drawing.Size(300, 300); - this.listBox.TabIndex = 0; + listBox.FormattingEnabled = true; + listBox.ItemHeight = 20; + listBox.Location = new Point(20, 18); + listBox.Margin = new Padding(3, 4, 3, 4); + listBox.Name = "listBox"; + listBox.Size = new Size(244, 264); + listBox.TabIndex = 0; // // ListBoxMany // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.Controls.Add(this.listBox); - this.Name = "ListBoxMany"; - this.Size = new System.Drawing.Size(300, 300); - this.ResumeLayout(false); - + AutoScaleMode = AutoScaleMode.None; + Controls.Add(listBox); + Margin = new Padding(3, 4, 3, 4); + Name = "ListBoxMany"; + Size = new Size(284, 306); + ResumeLayout(false); } #endregion diff --git a/COP_7/ListBoxMany.cs b/COP_7/ListBoxMany.cs index 29a6fed..0d8c116 100644 --- a/COP_7/ListBoxMany.cs +++ b/COP_7/ListBoxMany.cs @@ -4,105 +4,128 @@ using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace CustomComponent +namespace Components { public partial class ListBoxMany : UserControl { - private string LayoutString; - private string StartS; - private string EndS; - private int SelectedStr; - + /// + /// Конструктор по умолчанию + /// public ListBoxMany() { InitializeComponent(); } + /// + /// Макет + /// + private string _templateString; + + /// + /// Символ начала + /// + private string _startSymbol; + + /// + /// Символ конца + /// + private string _endSymbol; + + /// + /// Шаблон строки + /// + public void SetTemplateString(string templateString, string startSymbol, string endSymbol) + { + if (templateString != "" && startSymbol != "" && endSymbol != "") + { + _templateString = templateString; + _startSymbol = startSymbol; + _endSymbol = endSymbol; + } + else + { + throw new ArgumentNullException("Введены не все значения"); + } + } + + /// + /// Выбранная строка + /// public int SelectedIndex { - get - { - if (listBox.SelectedIndex == -1) - { - return -1; - } - return listBox.SelectedIndex; - } + get { return listBox.SelectedIndex; } set { - if (listBox.Items.Count != 0) + if (listBox.SelectedIndex != 0) { listBox.SelectedIndex = value; } } } - public void SetLayout(string layoutString, string startS, string endS) + + /// + /// Получение обхекта + /// + public T GetObjectFromStr() where T : class, new() { - if (layoutString == null || startS == null || endS == null) return; - LayoutString = layoutString; - StartS = startS; - EndS = endS; - } - public void AddItemInList(T Object, int rowIndex, int columnIndex) - { - if (Object == null) + if (listBox.SelectedIndex == -1) { - throw new ArgumentNullException(); + return null; } - - if (listBox.Items.Count <= rowIndex) + string row = listBox.SelectedItem.ToString(); + T curObject = new T(); + StringBuilder sb = new StringBuilder(row); + foreach (var property in typeof(T).GetProperties()) { - for (int i = listBox.Items.Count; i <= rowIndex; i++) - { - listBox.Items.Add(LayoutString); - } - } - - string str = listBox.Items[rowIndex].ToString(); - - var properties = Object.GetType().GetProperties(); - if (columnIndex < properties.Length) - { - var prop = properties[columnIndex]; - string str1 = $"{StartS}{prop.Name}{EndS}"; - str = str.Replace(str1, prop.GetValue(Object)?.ToString()); - } - - listBox.Items[rowIndex] = str; - } - - - public T GetItemFromList() where T : class, new() - { - string SelectedStr = ""; - if (listBox.SelectedIndex != -1) - { - SelectedStr = listBox.SelectedItem.ToString(); - } - - T currentObject = new T(); - - foreach (var prop in typeof(T).GetProperties()) - { - if (!prop.CanWrite) + if (!property.CanWrite) { continue; } - int startS = SelectedStr.IndexOf(StartS); - int endS = SelectedStr.IndexOf(EndS); - if (startS == -1 || endS == -1) + + int borderOne = sb.ToString().IndexOf(_startSymbol); + if (borderOne == -1) { break; } - string propValue = SelectedStr.Substring(startS + 1, endS - startS - 1); - SelectedStr = SelectedStr.Substring(endS + 1); - prop.SetValue(currentObject, Convert.ChangeType(propValue, prop.PropertyType)); + + int borderTwo = sb.ToString().IndexOf(_endSymbol, borderOne + 1); + if (borderTwo == -1) + { + break; + } + + string propertyValue = sb.ToString(borderOne + 1, borderTwo - borderOne - 1); + sb.Remove(0, borderTwo + 1); + property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType)); + } + return curObject; + } + + /// + /// Заполнение + /// + public void FillProperty(T dataObject, int rowIndex, string propertyName) + { + + while (listBox.Items.Count <= rowIndex) + { + listBox.Items.Add(_templateString); + } + + string row = listBox.Items[rowIndex].ToString(); + PropertyInfo propertyInfo = dataObject.GetType().GetProperty(propertyName); + + if (propertyInfo != null) + { + object propertyValue = propertyInfo.GetValue(dataObject); + row = row.Replace($"{_startSymbol}{propertyName}{_endSymbol}", propertyValue.ToString()); + listBox.Items[rowIndex] = row; } - return currentObject; } } -} +} \ No newline at end of file diff --git a/COP_7/ListBoxMany.resx b/COP_7/ListBoxMany.resx index f298a7b..af32865 100644 --- a/COP_7/ListBoxMany.resx +++ b/COP_7/ListBoxMany.resx @@ -1,4 +1,64 @@ - + + + diff --git a/COP_7/PhoneTextBox.cs b/COP_7/PhoneTextBox.cs deleted file mode 100644 index aa74d43..0000000 --- a/COP_7/PhoneTextBox.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using System.Text.RegularExpressions; - -namespace CustomComponent -{ - public partial class PhoneTextBox : UserControl - { - - - private string? pattern; - private string example = "+7(XXX)XXX-XX-XX"; - public PhoneTextBox() - { - InitializeComponent(); - } - - public string? Pattern - { - get { return pattern; } - set { pattern = value; } - } - - public string TextBoxValue - { - get - { - Regex regex = new Regex(Pattern); - bool isValid = regex.IsMatch(textBox.Text); - if (isValid) - { - return textBox.Text; - } - else - { - Error = "Неправильный ввод"; - return null; - } - } - set - { - Regex regex = new Regex(Pattern); - bool isValid = regex.IsMatch(value); - if (isValid) - { - textBox.Text = value; - } - else - { - Error = "Неправильно"; - } - } - } - - public string Error - { - get; private set; - } - public void SetExample(string exampleStr) - { - Regex regex = new Regex(Pattern); - bool isValid = regex.IsMatch(exampleStr); - if (isValid) - { - example = exampleStr; - } - } - - private void textBox_Enter(object sender, EventArgs e) - { - ToolTip tt = new ToolTip(); - tt.Show(example, textBox, 30, -20, 1000); - } - - private void textBox_TextChanged(object sender, EventArgs e) - { - _changeEvent?.Invoke(sender, e); - } - - private EventHandler _changeEvent; - public event EventHandler ChangeEvent - { - add - { - _changeEvent += value; - } - remove - { - _changeEvent -= value; - } - } - } -} diff --git a/COP_7/PhoneTextBox.resx b/COP_7/PhoneTextBox.resx deleted file mode 100644 index f298a7b..0000000 --- a/COP_7/PhoneTextBox.resx +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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_7/PhoneTextBox.Designer.cs b/COP_7/TextBoxPhone.Designer.cs similarity index 57% rename from COP_7/PhoneTextBox.Designer.cs rename to COP_7/TextBoxPhone.Designer.cs index 8120f23..01e8c8f 100644 --- a/COP_7/PhoneTextBox.Designer.cs +++ b/COP_7/TextBoxPhone.Designer.cs @@ -1,6 +1,6 @@ -namespace CustomComponent +namespace Components { - partial class PhoneTextBox + partial class TextBoxPhone { /// /// Обязательная переменная конструктора. @@ -28,33 +28,34 @@ /// private void InitializeComponent() { - this.textBox = new System.Windows.Forms.TextBox(); - this.SuspendLayout(); + components = new System.ComponentModel.Container(); + toolTipPhone = new ToolTip(components); + textBox = new TextBox(); + SuspendLayout(); // // textBox // - this.textBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; - this.textBox.Location = new System.Drawing.Point(3, 3); - this.textBox.Name = "textBox"; - this.textBox.Size = new System.Drawing.Size(100, 23); - this.textBox.TabIndex = 0; - this.textBox.TextChanged += textBox_TextChanged; - this.textBox.Enter += textBox_Enter; + textBox.Location = new Point(16, 22); + textBox.Name = "textBox"; + textBox.Size = new Size(168, 27); + textBox.TabIndex = 0; + textBox.Text = "+7XXXXXXXXXX"; + toolTipPhone.SetToolTip(textBox, "+7XXXXXXXXXX"); + textBox.Click += textBox_Enter; // - // PhoneTextBox + // TextBoxPhone // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.Controls.Add(this.textBox); - this.Name = "PhoneTextBox"; - this.Size = new System.Drawing.Size(108, 31); - this.ResumeLayout(false); - this.PerformLayout(); - + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(textBox); + Name = "TextBoxPhone"; + Size = new Size(210, 71); + ResumeLayout(false); + PerformLayout(); } #endregion - + private ToolTip toolTipPhone; private TextBox textBox; } } diff --git a/COP_7/TextBoxPhone.cs b/COP_7/TextBoxPhone.cs new file mode 100644 index 0000000..371e90c --- /dev/null +++ b/COP_7/TextBoxPhone.cs @@ -0,0 +1,111 @@ +using Components.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 Components +{ + public partial class TextBoxPhone : UserControl + { + /// + /// Конструктор + /// + public TextBoxPhone() + { + InitializeComponent(); + } + + /// + /// Шаблон вводимого значения + /// + private string? _phonePattern; + + /// + /// Шаблон вводимого значения + /// + public string? PhonePattern + { + get { return _phonePattern; } + set { _phonePattern = value; } + } + + /// + /// Введенное значение + /// + public string? PhoneTextBox + { + get + { + if (PhonePattern == null) + { + throw new PhoneException("Шаблон не заполнен!"); + } + + Regex regex = new Regex(PhonePattern); + if (regex.IsMatch(textBox.Text)) + { + return textBox.Text; + } + else + { + throw new PhoneException(textBox.Text + " не соответствует шаблону!"); + } + } + + set + { + Regex regex = new Regex(PhonePattern!); + if (regex.IsMatch(value)) + { + textBox.Text = value; + } + + } + } + + /// + /// Событие, вызываемое при смене значения + /// + private EventHandler _onValueChangedEvent; + + /// + /// Событие, вызываемое при смене значения + /// + public event EventHandler ValueChanged + { + add + { + _onValueChangedEvent += value; + } + + remove + { + _onValueChangedEvent -= value; + } + } + /// + /// Смена значения + /// + private void CustomPhoneBox_PhoneChanged(object sender, EventArgs e) + { + _onValueChangedEvent?.Invoke(sender, e); + } + + /// + /// Выведение подсказки на экран + /// + private void textBox_Enter(object sender, EventArgs e) + { + int visibleTime = 2000; + ToolTip tooltip = new ToolTip(); + tooltip.Show("+7XXXXXXXXXX", textBox, 30, -20, visibleTime); + } + } +} diff --git a/COP_7/TextBoxPhone.resx b/COP_7/TextBoxPhone.resx new file mode 100644 index 0000000..471c9da --- /dev/null +++ b/COP_7/TextBoxPhone.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 17, 17 + + \ No newline at end of file diff --git a/FormTest/Form1.Designer.cs b/FormTest/Form1.Designer.cs index b5c42b4..c2d2fa5 100644 --- a/FormTest/Form1.Designer.cs +++ b/FormTest/Form1.Designer.cs @@ -3,12 +3,12 @@ partial class Form1 { /// - /// Required designer variable. + /// Required designer variable. /// private System.ComponentModel.IContainer components = null; /// - /// Clean up any resources being used. + /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) @@ -23,27 +23,48 @@ #region Windows Form Designer generated code /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. /// private void InitializeComponent() { - dropDownList1 = new COP_7.DropDownList(); + dropDownList1 = new Components.DropDownList(); + listBoxMany1 = new Components.ListBoxMany(); + textBoxPhone2 = new Components.TextBoxPhone(); SuspendLayout(); // // dropDownList1 // - dropDownList1.Location = new Point(26, 12); + dropDownList1.Location = new Point(12, 22); dropDownList1.Name = "dropDownList1"; - dropDownList1.SelectedValue = ""; - dropDownList1.Size = new Size(157, 46); + dropDownList1.SelectedItem = " "; + dropDownList1.Size = new Size(188, 46); dropDownList1.TabIndex = 0; // + // listBoxMany1 + // + listBoxMany1.Location = new Point(225, 22); + listBoxMany1.Margin = new Padding(3, 4, 3, 4); + listBoxMany1.Name = "listBoxMany1"; + listBoxMany1.SelectedIndex = -1; + listBoxMany1.Size = new Size(222, 223); + listBoxMany1.TabIndex = 1; + // + // textBoxPhone2 + // + textBoxPhone2.Location = new Point(519, 22); + textBoxPhone2.Name = "textBoxPhone2"; + textBoxPhone2.PhonePattern = null; + textBoxPhone2.Size = new Size(258, 145); + textBoxPhone2.TabIndex = 3; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(800, 450); + Controls.Add(textBoxPhone2); + Controls.Add(listBoxMany1); Controls.Add(dropDownList1); Name = "Form1"; Text = "Form1"; @@ -52,6 +73,8 @@ #endregion - private COP_7.DropDownList dropDownList1; + private Components.DropDownList dropDownList1; + private Components.ListBoxMany listBoxMany1; + private Components.TextBoxPhone textBoxPhone2; } -} +} \ No newline at end of file diff --git a/FormTest/Form1.cs b/FormTest/Form1.cs index 296891b..71046e4 100644 --- a/FormTest/Form1.cs +++ b/FormTest/Form1.cs @@ -1,3 +1,13 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + namespace FormTest { public partial class Form1 : Form