From 17102989aeed19fd04866367749d73df063a5f2e Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Fri, 15 Sep 2023 22:21:56 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MyListBoxObjects.Designer.cs | 59 +++++++++ COP/VisualComponentsLib/MyListBoxObjects.cs | 104 +++++++++++++++ COP/VisualComponentsLib/MyListBoxObjects.resx | 120 ++++++++++++++++++ COP/VisualComponentsLib/Object/Student.cs | 27 ++++ .../VisualComponentsLib.csproj | 10 ++ COP/WinForms/Form1.Designer.cs | 50 ++++++++ COP/WinForms/Form1.cs | 22 ++++ 7 files changed, 392 insertions(+) create mode 100644 COP/VisualComponentsLib/MyListBoxObjects.Designer.cs create mode 100644 COP/VisualComponentsLib/MyListBoxObjects.cs create mode 100644 COP/VisualComponentsLib/MyListBoxObjects.resx create mode 100644 COP/VisualComponentsLib/Object/Student.cs diff --git a/COP/VisualComponentsLib/MyListBoxObjects.Designer.cs b/COP/VisualComponentsLib/MyListBoxObjects.Designer.cs new file mode 100644 index 0000000..842600d --- /dev/null +++ b/COP/VisualComponentsLib/MyListBoxObjects.Designer.cs @@ -0,0 +1,59 @@ +namespace VisualComponentsLib +{ + partial class MyListBoxObjects + { + /// + /// Обязательная переменная конструктора. + /// + 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() + { + this.listBoxObj = new System.Windows.Forms.ListBox(); + this.SuspendLayout(); + // + // listBoxObj + // + this.listBoxObj.FormattingEnabled = true; + this.listBoxObj.HorizontalScrollbar = true; + this.listBoxObj.ItemHeight = 16; + this.listBoxObj.Location = new System.Drawing.Point(3, 3); + this.listBoxObj.Name = "listBoxObj"; + this.listBoxObj.Size = new System.Drawing.Size(353, 116); + this.listBoxObj.TabIndex = 0; + // + // MyListBoxObjects + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.listBoxObj); + this.Name = "MyListBoxObjects"; + this.Size = new System.Drawing.Size(359, 133); + this.ResumeLayout(false); + + } + + #endregion + + private System.Windows.Forms.ListBox listBoxObj; + } +} diff --git a/COP/VisualComponentsLib/MyListBoxObjects.cs b/COP/VisualComponentsLib/MyListBoxObjects.cs new file mode 100644 index 0000000..e65555c --- /dev/null +++ b/COP/VisualComponentsLib/MyListBoxObjects.cs @@ -0,0 +1,104 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows.Forms; + +namespace VisualComponentsLib +{ + public partial class MyListBoxObjects : UserControl + { + //Макетная строка + private string layoutString; + //начальный символ для обнаружения свойств/полей + private string startSymbol; + //конечный символ для обнаружения свойств/полей + private string endSymbol; + public MyListBoxObjects() + { + InitializeComponent(); + } + //Метод для установки информации о макетной строке и символах (начального и конечного) + public void SetLayoutInfo(string layout, string startS, string endS) + { + if (layout == null || startS == null || endS == null) + { + return; + } + layoutString = layout; + startSymbol = startS; + endSymbol = endS; + } + + //свойство для получения и заполнения индекса выбранного элемента + public int SelectedIndex + { + get + { + if (listBoxObj.SelectedIndex == -1) + { + return -1; + } + return listBoxObj.SelectedIndex; + } + set + { + if (listBoxObj.SelectedItems.Count != 0) + { + listBoxObj.SelectedIndex = value; + } + } + } + + //Публичный параметризованный метод для получения объекта из выбранной строки(создать объект и через рефлексию заполнить свойства его). + public T GetObjectFromStr() where T : class, new() + { + string selStr = ""; + if (listBoxObj.SelectedIndex != -1) + { + selStr = listBoxObj.SelectedItem.ToString(); + } + T curObject = new T(); + foreach (var pr in typeof(T).GetProperties()) + { + if (!pr.CanWrite) + { + continue; + } + int borderOne = selStr.IndexOf(startSymbol); + StringBuilder sb = new StringBuilder(selStr); + sb[borderOne] = 'z'; + selStr = sb.ToString(); + int borderTwo = selStr.IndexOf(endSymbol); + if (borderOne == -1 || borderTwo == -1) break; + string propertyValue = selStr.Substring(borderOne + 1, borderTwo - borderOne - 1); + selStr = selStr.Substring(borderTwo + 1); + pr.SetValue(curObject, Convert.ChangeType(propertyValue, pr.PropertyType)); + } + return curObject; + } + //параметризованный метод, у которого в передаваемых параметрах идет список объектов некого класса и через этот список идет заполнение ListBox; + public void AddInListBox(List objects) + { + if (layoutString == null || startSymbol == null || endSymbol == null) + { + MessageBox.Show("заполните информацию о макетной строке"); + return; + } + if (!layoutString.Contains(startSymbol) || !layoutString.Contains(endSymbol)) + { + MessageBox.Show("Макетная строка не содержит нужные элементы"); + return; + } + foreach (var item in objects) + { + string str = layoutString; + foreach (var prop in item.GetType().GetProperties()) + { + string str1 = $"{startSymbol}" + $"{prop.Name}" + $"{endSymbol}"; + str = str.Replace(str1, $"{startSymbol}" + prop.GetValue(item).ToString() + $"{endSymbol}"); + } + listBoxObj.Items.Add(str); + } + } + } +} diff --git a/COP/VisualComponentsLib/MyListBoxObjects.resx b/COP/VisualComponentsLib/MyListBoxObjects.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/COP/VisualComponentsLib/MyListBoxObjects.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/VisualComponentsLib/Object/Student.cs b/COP/VisualComponentsLib/Object/Student.cs new file mode 100644 index 0000000..4421128 --- /dev/null +++ b/COP/VisualComponentsLib/Object/Student.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace VisualComponentsLib.Object +{ + public class Student + { + public string Name { get; set; } + public string Group { get; set; } + public string Faculty { get; set; } + public int Course { get; set; } + + public Student(string name, string group, string faculty, int course) + { + Name = name; + Group = group; + Faculty = faculty; + Course = course; + } + public Student() + { + } + } +} diff --git a/COP/VisualComponentsLib/VisualComponentsLib.csproj b/COP/VisualComponentsLib/VisualComponentsLib.csproj index b20a97a..dca6737 100644 --- a/COP/VisualComponentsLib/VisualComponentsLib.csproj +++ b/COP/VisualComponentsLib/VisualComponentsLib.csproj @@ -54,6 +54,13 @@ MyEmailTextBox.cs + + UserControl + + + MyListBoxObjects.cs + + @@ -64,6 +71,9 @@ MyEmailTextBox.cs + + MyListBoxObjects.cs + \ No newline at end of file diff --git a/COP/WinForms/Form1.Designer.cs b/COP/WinForms/Form1.Designer.cs index 71a5cb5..a5198c0 100644 --- a/COP/WinForms/Form1.Designer.cs +++ b/COP/WinForms/Form1.Designer.cs @@ -39,6 +39,10 @@ buttonSetExample = new Button(); labelExample = new Label(); textBoxExample = new TextBox(); + listBoxObj = new VisualComponentsLib.MyListBoxObjects(); + buttonAddObjects = new Button(); + buttonShowItem = new Button(); + labelShowInput = new Label(); SuspendLayout(); // // dropDownList @@ -143,6 +147,44 @@ textBoxExample.Size = new Size(188, 27); textBoxExample.TabIndex = 6; // + // listBoxObj + // + listBoxObj.Location = new Point(388, 13); + listBoxObj.Margin = new Padding(3, 4, 3, 4); + listBoxObj.Name = "listBoxObj"; + listBoxObj.SelectedIndex = -1; + listBoxObj.Size = new Size(368, 159); + listBoxObj.TabIndex = 11; + // + // buttonAddObjects + // + buttonAddObjects.Location = new Point(388, 179); + buttonAddObjects.Name = "buttonAddObjects"; + buttonAddObjects.Size = new Size(380, 29); + buttonAddObjects.TabIndex = 12; + buttonAddObjects.Text = "Добавить"; + buttonAddObjects.UseVisualStyleBackColor = true; + buttonAddObjects.Click += buttonAddObjects_Click; + // + // buttonShowItem + // + buttonShowItem.Location = new Point(388, 310); + buttonShowItem.Name = "buttonShowItem"; + buttonShowItem.Size = new Size(380, 29); + buttonShowItem.TabIndex = 13; + buttonShowItem.Text = "Получить"; + buttonShowItem.UseVisualStyleBackColor = true; + buttonShowItem.Click += buttonShowItem_Click; + // + // labelShowInput + // + labelShowInput.AutoSize = true; + labelShowInput.Location = new Point(388, 273); + labelShowInput.Name = "labelShowInput"; + labelShowInput.Size = new Size(54, 20); + labelShowInput.TabIndex = 14; + labelShowInput.Text = "Вывод"; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); @@ -159,6 +201,10 @@ Controls.Add(buttonSetExample); Controls.Add(labelShow); Controls.Add(buttonShow); + Controls.Add(listBoxObj); + Controls.Add(buttonAddObjects); + Controls.Add(buttonShowItem); + Controls.Add(labelShowInput); Name = "Form1"; Text = "Form1"; ResumeLayout(false); @@ -177,5 +223,9 @@ private Button buttonSetExample; private Label labelShow; private Button buttonShow; + private VisualComponentsLib.MyListBoxObjects listBoxObj; + private Button buttonAddObjects; + private Button buttonShowItem; + private Label labelShowInput; } } \ No newline at end of file diff --git a/COP/WinForms/Form1.cs b/COP/WinForms/Form1.cs index f9d026f..d85d5f0 100644 --- a/COP/WinForms/Form1.cs +++ b/COP/WinForms/Form1.cs @@ -1,15 +1,26 @@ +using System.Security.Cryptography; +using VisualComponentsLib.Object; + namespace WinForms { public partial class Form1 : Form { List list = new List(); + List students = new List(); public Form1() { list = new List(); list.AddRange(new string[] { "", "", "" }); + Student student1 = new Student("", "-32", "", 3); + Student student2 = new Student("", "-11", "", 1); + Student student3 = new Student("", "-41", "", 4); + students.Add(student1); + students.Add(student2); + students.Add(student3); InitializeComponent(); dropDownList.LoadValues(new List() { "", "", "" }); emailTextBox.Pattern = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"; + listBoxObj.SetLayoutInfo(" *Name* *Group* *Faculty* *Course*", "*", "*"); } private void buttonAdd_Click(object sender, EventArgs e) { @@ -49,5 +60,16 @@ namespace WinForms MessageBox.Show(ex.Message); } } + + private void buttonAddObjects_Click(object sender, EventArgs e) + { + listBoxObj.AddInListBox(students); + } + + private void buttonShowItem_Click(object sender, EventArgs e) + { + string str = listBoxObj.GetObjectFromStr().Name + " " + listBoxObj.GetObjectFromStr().Group + " " + listBoxObj.GetObjectFromStr().Faculty + " " + listBoxObj.GetObjectFromStr().Course; + labelShowInput.Text = str; + } } } \ No newline at end of file