From cf9dc68b637dfbf5389bc8e2252bfba1b1e53155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F?= <Илья@WIN-RANNDDD> Date: Sat, 21 Sep 2024 11:26:26 +0400 Subject: [PATCH] lab1 ready --- .../VisualComponents/ListBoxControl.cs | 60 +++++- .../VisualComponents/TextBoxControl.cs | 15 +- COP/WinForms/FormTest.Designer.cs | 175 +++++++++++++++++- COP/WinForms/FormTest.cs | 58 ++++++ COP/WinForms/FormTest.resx | 54 +++--- COP/WinForms/Person.cs | 13 ++ COP/WinForms/WinForms.csproj | 4 + 7 files changed, 338 insertions(+), 41 deletions(-) create mode 100644 COP/WinForms/Person.cs diff --git a/COP/RodionovLibrary/VisualComponents/ListBoxControl.cs b/COP/RodionovLibrary/VisualComponents/ListBoxControl.cs index e154a99..22634b8 100644 --- a/COP/RodionovLibrary/VisualComponents/ListBoxControl.cs +++ b/COP/RodionovLibrary/VisualComponents/ListBoxControl.cs @@ -1,4 +1,6 @@ -namespace RodionovLibrary.VisualComponents +using System.Reflection; + +namespace RodionovLibrary.VisualComponents { public partial class ListBoxControl : UserControl { @@ -31,5 +33,61 @@ _fromChar = fromChar; _toChar = toChar; } + + public T? GetObject() + { + if (listBox.SelectedIndex == -1 || string.IsNullOrEmpty(_template) || !_fromChar.HasValue || !_toChar.HasValue) + throw new ArgumentException("Не хватает данных"); + + var type = typeof(T); + var properties = type.GetProperties(); + var curObject = Activator.CreateInstance(type); + + string[] wordsTemplate = _template.Split(' '); + string[] words = listBox.SelectedItem.ToString().Split(' '); + + for (int i = 0; i < wordsTemplate.Length; i++) + { + string word = wordsTemplate[i]; + if (word.Contains(_fromChar.Value) && word.Contains(_toChar.Value)) + { + int startCharIndex = word.IndexOf(_fromChar.Value); + int endCharIndex = word.LastIndexOf(_toChar.Value); + + string propertyName = word.Substring(startCharIndex + 1, endCharIndex - startCharIndex - 1); + var property = properties.FirstOrDefault(x => x.Name == propertyName); + if (property == null) + continue; + + int extraCharsBefore = startCharIndex; + int extraCharsAfter = word.Length - endCharIndex - 1; + + string propertyValue = words[i].Substring(extraCharsBefore, + words[i].Length - extraCharsBefore - extraCharsAfter); + property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType)); + } + } + return (T?)curObject; + } + + public void AddItems(List items) + where T : class + { + if (string.IsNullOrEmpty(_template) || !_fromChar.HasValue || !_toChar.HasValue) + throw new ArgumentException("Не хватает данных"); + listBox.Items.Clear(); + var type = typeof(T); + var properties = type.GetProperties(); + foreach (T item in items) + { + string result = _template; + foreach (var property in properties) + { + string search = _fromChar.Value + property.Name + _toChar.Value; + result = result.Replace(search, property.GetValue(item)?.ToString() ?? ""); + } + listBox.Items.Add(result); + } + } } } diff --git a/COP/RodionovLibrary/VisualComponents/TextBoxControl.cs b/COP/RodionovLibrary/VisualComponents/TextBoxControl.cs index 7880d4e..8a2e9d2 100644 --- a/COP/RodionovLibrary/VisualComponents/TextBoxControl.cs +++ b/COP/RodionovLibrary/VisualComponents/TextBoxControl.cs @@ -21,19 +21,16 @@ namespace RodionovLibrary.VisualComponents public string Value { get { - if (Pattern == null) - throw new NullPatternException("Не задан шаблон!"); + if (string.IsNullOrEmpty(Pattern)) + throw new NullPatternException("Не задан шаблон"); if (!new Regex(Pattern).IsMatch(textBox.Text)) - throw new NotMatchPatternException("Введенное значение не соответствует шаблону!"); + throw new NotMatchPatternException("Введенное значение не соответствует шаблону"); return textBox.Text; } set { - if (Pattern == null) - throw new NullPatternException("Не задан шаблон!"); - if (!new Regex(Pattern).IsMatch(value)) - throw new NotMatchPatternException("Введенное значение не соответствует шаблону!"); - textBox.Text = value; + if (!string.IsNullOrEmpty(Pattern) && new Regex(Pattern).IsMatch(value)) + textBox.Text = value; } } @@ -55,7 +52,7 @@ namespace RodionovLibrary.VisualComponents private void TextBox_MouseEnter(object sender, EventArgs e) { - toolTip.Show(tooltipText ?? "", textBox); + toolTip.Show(tooltipText ?? "", textBox, 45, 20, 3000); } } } diff --git a/COP/WinForms/FormTest.Designer.cs b/COP/WinForms/FormTest.Designer.cs index 8f53682..a7fea44 100644 --- a/COP/WinForms/FormTest.Designer.cs +++ b/COP/WinForms/FormTest.Designer.cs @@ -28,12 +28,179 @@ /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "Form1"; + comboBoxControl = new RodionovLibrary.VisualComponents.ComboBoxControl(); + listBoxControl = new RodionovLibrary.VisualComponents.ListBoxControl(); + textBoxControl = new RodionovLibrary.VisualComponents.TextBoxControl(); + buttonClear = new Button(); + buttonGetComboBox = new Button(); + buttonSetComboBox = new Button(); + buttonGetTextBox = new Button(); + buttonSetTextBox = new Button(); + buttonSetWrongTextBox = new Button(); + buttonGetObject = new Button(); + buttonGetIndex = new Button(); + buttonSetIndex = new Button(); + SuspendLayout(); + // + // comboBoxControl + // + comboBoxControl.AutoSize = true; + comboBoxControl.AutoSizeMode = AutoSizeMode.GrowAndShrink; + comboBoxControl.Location = new Point(45, 27); + comboBoxControl.Margin = new Padding(3, 4, 3, 4); + comboBoxControl.Name = "comboBoxControl"; + comboBoxControl.SelectedValue = ""; + comboBoxControl.Size = new Size(210, 32); + comboBoxControl.TabIndex = 0; + // + // listBoxControl + // + listBoxControl.AutoSize = true; + listBoxControl.AutoSizeMode = AutoSizeMode.GrowAndShrink; + listBoxControl.Location = new Point(45, 276); + listBoxControl.Margin = new Padding(3, 4, 3, 4); + listBoxControl.Name = "listBoxControl"; + listBoxControl.SelectedIndex = -1; + listBoxControl.Size = new Size(647, 488); + listBoxControl.TabIndex = 1; + // + // textBoxControl + // + textBoxControl.AutoSize = true; + textBoxControl.AutoSizeMode = AutoSizeMode.GrowAndShrink; + textBoxControl.Location = new Point(446, 27); + textBoxControl.Margin = new Padding(3, 4, 3, 4); + textBoxControl.Name = "textBoxControl"; + textBoxControl.Pattern = null; + textBoxControl.Size = new Size(246, 31); + textBoxControl.TabIndex = 2; + // + // buttonClear + // + buttonClear.Location = new Point(47, 66); + buttonClear.Name = "buttonClear"; + buttonClear.Size = new Size(94, 29); + buttonClear.TabIndex = 3; + buttonClear.Text = "Отчистка"; + buttonClear.UseVisualStyleBackColor = true; + buttonClear.Click += ButtonClear_Click; + // + // buttonGetComboBox + // + buttonGetComboBox.Location = new Point(47, 114); + buttonGetComboBox.Name = "buttonGetComboBox"; + buttonGetComboBox.Size = new Size(165, 29); + buttonGetComboBox.TabIndex = 4; + buttonGetComboBox.Text = "Получение значения"; + buttonGetComboBox.UseVisualStyleBackColor = true; + buttonGetComboBox.Click += ButtonGetComboBox_Click; + // + // buttonSetComboBox + // + buttonSetComboBox.Location = new Point(47, 165); + buttonSetComboBox.Name = "buttonSetComboBox"; + buttonSetComboBox.Size = new Size(165, 29); + buttonSetComboBox.TabIndex = 5; + buttonSetComboBox.Text = "Установка значения"; + buttonSetComboBox.UseVisualStyleBackColor = true; + buttonSetComboBox.Click += ButtonSetComboBox_Click; + // + // buttonGetTextBox + // + buttonGetTextBox.Location = new Point(446, 66); + buttonGetTextBox.Name = "buttonGetTextBox"; + buttonGetTextBox.Size = new Size(165, 29); + buttonGetTextBox.TabIndex = 6; + buttonGetTextBox.Text = "Получение значения"; + buttonGetTextBox.UseVisualStyleBackColor = true; + buttonGetTextBox.Click += ButtonGetTextBox_Click; + // + // buttonSetTextBox + // + buttonSetTextBox.Location = new Point(446, 114); + buttonSetTextBox.Name = "buttonSetTextBox"; + buttonSetTextBox.Size = new Size(165, 29); + buttonSetTextBox.TabIndex = 7; + buttonSetTextBox.Text = "Установка значения"; + buttonSetTextBox.UseVisualStyleBackColor = true; + buttonSetTextBox.Click += ButtonSetTextBox_Click; + // + // buttonSetWrongTextBox + // + buttonSetWrongTextBox.Location = new Point(446, 165); + buttonSetWrongTextBox.Name = "buttonSetWrongTextBox"; + buttonSetWrongTextBox.Size = new Size(246, 29); + buttonSetWrongTextBox.TabIndex = 8; + buttonSetWrongTextBox.Text = "Установка значения (неверное)"; + buttonSetWrongTextBox.UseVisualStyleBackColor = true; + buttonSetWrongTextBox.Click += ButtonSetWrongTextBox_Click; + // + // buttonGetObject + // + buttonGetObject.Location = new Point(699, 463); + buttonGetObject.Name = "buttonGetObject"; + buttonGetObject.Size = new Size(165, 29); + buttonGetObject.TabIndex = 9; + buttonGetObject.Text = "Получение объекта"; + buttonGetObject.UseVisualStyleBackColor = true; + buttonGetObject.Click += ButtonGetObject_Click; + // + // buttonGetIndex + // + buttonGetIndex.Location = new Point(699, 516); + buttonGetIndex.Name = "buttonGetIndex"; + buttonGetIndex.Size = new Size(165, 29); + buttonGetIndex.TabIndex = 10; + buttonGetIndex.Text = "Получение индекса"; + buttonGetIndex.UseVisualStyleBackColor = true; + buttonGetIndex.Click += ButtonGetIndex_Click; + // + // buttonSetIndex + // + buttonSetIndex.Location = new Point(699, 569); + buttonSetIndex.Name = "buttonSetIndex"; + buttonSetIndex.Size = new Size(165, 29); + buttonSetIndex.TabIndex = 11; + buttonSetIndex.Text = "Установка индекса"; + buttonSetIndex.UseVisualStyleBackColor = true; + buttonSetIndex.Click += ButtonSetIndex_Click; + // + // FormTest + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(878, 787); + Controls.Add(buttonSetIndex); + Controls.Add(buttonGetIndex); + Controls.Add(buttonGetObject); + Controls.Add(buttonSetWrongTextBox); + Controls.Add(buttonSetTextBox); + Controls.Add(buttonGetTextBox); + Controls.Add(buttonSetComboBox); + Controls.Add(buttonGetComboBox); + Controls.Add(buttonClear); + Controls.Add(textBoxControl); + Controls.Add(listBoxControl); + Controls.Add(comboBoxControl); + Name = "FormTest"; + Text = "FormTest"; + ResumeLayout(false); + PerformLayout(); } #endregion + + private RodionovLibrary.VisualComponents.ComboBoxControl comboBoxControl; + private RodionovLibrary.VisualComponents.ListBoxControl listBoxControl; + private RodionovLibrary.VisualComponents.TextBoxControl textBoxControl; + private Button buttonClear; + private Button buttonGetComboBox; + private Button buttonSetComboBox; + private Button buttonGetTextBox; + private Button buttonSetTextBox; + private Button buttonSetWrongTextBox; + private Button buttonGetObject; + private Button buttonGetIndex; + private Button buttonSetIndex; } } diff --git a/COP/WinForms/FormTest.cs b/COP/WinForms/FormTest.cs index ceaa606..c5ff199 100644 --- a/COP/WinForms/FormTest.cs +++ b/COP/WinForms/FormTest.cs @@ -5,6 +5,64 @@ namespace WinForms public FormTest() { InitializeComponent(); + comboBoxControl.AddItems(new List { " 1", " 2", " 3", " 4", " 5" }); + + textBoxControl.Pattern = @"^[a-z0-9._%+-]+\@([a-z0-9-]+\.)+[a-z]{2,4}$"; + textBoxControl.SetTooltipText("example@gmail.com"); + + listBoxControl.SetParams(": {FirstName}, : {LastName}. {Gender} ({Age}) .", '{', '}'); + listBoxControl.AddItems(new List { new() { FirstName = "", LastName = "", Age = 23, Gender = "" }, + new() { FirstName = "", LastName = "", Age = 18, Gender = "" }, + new() { FirstName = "", LastName = "", Age = 40, Gender = "" } }); + } + + private void ButtonClear_Click(object sender, EventArgs e) + { + comboBoxControl.Clear(); + } + + private void ButtonGetComboBox_Click(object sender, EventArgs e) + { + MessageBox.Show(comboBoxControl.SelectedValue, " "); + } + + private void ButtonSetComboBox_Click(object sender, EventArgs e) + { + comboBoxControl.SelectedValue = " 3"; + } + + private void ButtonGetTextBox_Click(object sender, EventArgs e) + { + MessageBox.Show(textBoxControl.Value, " "); + } + + private void ButtonSetTextBox_Click(object sender, EventArgs e) + { + textBoxControl.Value = "forum98761@gmail.com"; + } + + private void ButtonSetWrongTextBox_Click(object sender, EventArgs e) + { + textBoxControl.Value = "smth"; + } + + private void ButtonGetObject_Click(object sender, EventArgs e) + { + Person? selectedPerson = listBoxControl.GetObject(); + if (selectedPerson == null) + MessageBox.Show(" "); + MessageBox.Show($": {selectedPerson?.FirstName}, : {selectedPerson?.LastName}, " + + $": {selectedPerson?.Age}, : {selectedPerson?.Gender}"); + } + + private void ButtonGetIndex_Click(object sender, EventArgs e) + { + MessageBox.Show(listBoxControl.SelectedIndex.ToString(), " "); + } + + private void ButtonSetIndex_Click(object sender, EventArgs e) + { + listBoxControl.SelectedIndex = 0; } } } diff --git a/COP/WinForms/FormTest.resx b/COP/WinForms/FormTest.resx index 1af7de1..8b2ff64 100644 --- a/COP/WinForms/FormTest.resx +++ b/COP/WinForms/FormTest.resx @@ -1,17 +1,17 @@  - diff --git a/COP/WinForms/Person.cs b/COP/WinForms/Person.cs new file mode 100644 index 0000000..f65b142 --- /dev/null +++ b/COP/WinForms/Person.cs @@ -0,0 +1,13 @@ +namespace WinForms +{ + public class Person + { + public string FirstName { get; set; } = string.Empty; + + public string LastName { get; set; } = string.Empty; + + public int Age { get; set; } + + public string Gender { get; set; } = string.Empty; + } +} diff --git a/COP/WinForms/WinForms.csproj b/COP/WinForms/WinForms.csproj index b57c89e..fe0dd2f 100644 --- a/COP/WinForms/WinForms.csproj +++ b/COP/WinForms/WinForms.csproj @@ -8,4 +8,8 @@ enable + + + + \ No newline at end of file