From ff464aea76d08654f8ffc22d29c863e3e8cd6674 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Mon, 14 Oct 2024 20:12:04 +0400 Subject: [PATCH 1/2] lab1 - modifed --- .../WinFormsApp1/Form1.Designer.cs | 1 - .../WinFormsLibrary1/ListBox.Designer.cs | 2 +- WinFormsLibrary1/WinFormsLibrary1/ListBox.cs | 106 +++++++++++------- 3 files changed, 64 insertions(+), 45 deletions(-) diff --git a/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs b/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs index e168305..95899d2 100644 --- a/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs +++ b/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs @@ -41,7 +41,6 @@ namespace WinFormsApp1 // // dateInputControl1 // - dateInputControl1.Date = ""; dateInputControl1.Location = new Point(13, 107); dateInputControl1.Margin = new Padding(4, 5, 4, 5); dateInputControl1.Name = "dateInputControl1"; diff --git a/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs b/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs index 2d5e613..5331df7 100644 --- a/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs +++ b/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs @@ -25,7 +25,7 @@ this.listBox.Name = "listBox"; this.listBox.Size = new System.Drawing.Size(120, 95); this.listBox.TabIndex = 0; - this.listBox.SelectedIndexChanged += new System.EventHandler(this.listBox_SelectedIndexChanged); + //this.listBox.SelectedIndexChanged += new System.EventHandler(this.listBox_SelectedIndexChanged); // // ListBoxUserControl // diff --git a/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs b/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs index fb0d8d8..715c98a 100644 --- a/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs +++ b/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; @@ -10,9 +11,9 @@ namespace WinFormsLibrary1 { private string? _template; - private string _startSymbol; + private char? _startSymbol; - private string _endSymbol; + private char? _endSymbol; public int SelectedIndex { get @@ -28,7 +29,7 @@ namespace WinFormsLibrary1 { InitializeComponent(); } - public void SetParams(string template, string fromChar, string toChar) + public void SetParams(string template, char? fromChar, char? toChar) { _template = template; _startSymbol = fromChar; @@ -37,62 +38,81 @@ namespace WinFormsLibrary1 public T? GetObject() where T : class, new() { - if (listBox.SelectedIndex == -1) + if (listBox.SelectedIndex == -1 || string.IsNullOrEmpty(_template) || !_startSymbol.HasValue || !_endSymbol.HasValue) + throw new ArgumentException("Не хватает данных"); + + var type = typeof(T); + var fields = type.GetFields(); + var properties = type.GetProperties(); + var members = fields.Cast().Concat(properties.Cast()).ToArray(); + + var curObject = new T(); + string text = listBox.SelectedItem?.ToString() ?? ""; + + var words = System.Text.RegularExpressions.Regex.Split(_template, $@"\{_startSymbol.Value}.*?\{_endSymbol.Value}"); + + int firstWordStart = text.IndexOf(words[0], 0); + if (firstWordStart == -1) + throw new Exception("Не найден элемент шаблона"); + if (firstWordStart != 0) { - return null; + string beginning = text[..firstWordStart]; + FillMember(_template.Substring(1, firstWordStart - 2), curObject, beginning, members); } - string selectedString = listBox.SelectedItem.ToString()!; - T obj = new T(); + int start = 0; - string pattern = $@"{Regex.Escape(_startSymbol)}(.*?){Regex.Escape(_endSymbol)}"; - MatchCollection matches = Regex.Matches(_template, pattern); - - string[] words = selectedString.Split(' '); - - int wordIndex = 0; - - foreach (Match match in matches) + for (int i = 0; i < words.Length - 1; i++) { - string propertyName = match.Groups[1].Value; - var property = obj.GetType().GetProperty(propertyName); - if (property != null && property.CanWrite) - { - if (wordIndex < words.Length) - { - string propertyValue = words[wordIndex]; - property.SetValue(obj, Convert.ChangeType(propertyValue, property.PropertyType)); - wordIndex++; - } - } + start = text.IndexOf(words[i], start); + if (start == -1) + throw new Exception("Не найден элемент шаблона"); + start += words[i].Length; + + int nextWordIndex = text.IndexOf(words[i + 1], start); + if (nextWordIndex == -1) + throw new Exception("Не найден следующий элемент шаблона"); + + string valueBetween = text[start..nextWordIndex]; + + string layoutPart = _template.Substring(_template.IndexOf(words[i]) + words[i].Length); + int startCharIndex = layoutPart.IndexOf(_startSymbol.Value); + int endCharIndex = layoutPart.IndexOf(_endSymbol.Value); + string memberName = layoutPart.Substring(startCharIndex + 1, endCharIndex - startCharIndex - 1); + + FillMember(memberName, curObject, valueBetween, members); + + start = nextWordIndex; } - return obj; + return (T?)curObject; + } - public void AddObject(T obj) + private void SetMemberValue(object obj, MemberInfo member, object value) { - if (obj == null) + if (member is PropertyInfo property) { - throw new ArgumentNullException("Добавляемый объект не существует!"); + property.SetValue(obj, value); } - if (string.IsNullOrEmpty(_template) || string.IsNullOrEmpty(_startSymbol) || string.IsNullOrEmpty(_endSymbol)) + else if (member is FieldInfo field) { - throw new Exception("Заполните макетную строку!"); - } - if (!_template.Contains(_startSymbol) || !_template.Contains(_endSymbol)) - { - throw new Exception("Макетная строка не содержит нужные элементы!"); + field.SetValue(obj, value); } + } - string processedString = _template; - foreach (var property in obj.GetType().GetProperties()) - { - string placeholder = $"{_startSymbol}{property.Name}{_endSymbol}"; - processedString = processedString.Replace(placeholder, $"{_startSymbol}{property.GetValue(obj)}{_endSymbol}"); - } + private void FillMember(string memberName, object curObject, string value, MemberInfo[]? members) + { + var member = members?.FirstOrDefault(x => x.Name == memberName) + ?? throw new Exception("Ошибка с поиском элемента"); + object convertedValue = Convert.ChangeType(value, GetMemberType(member)); - listBox.Items.Add(processedString); + SetMemberValue(curObject, member, convertedValue); + } + + private Type GetMemberType(MemberInfo member) + { + return member is PropertyInfo property ? property.PropertyType : ((FieldInfo)member).FieldType; } } } \ No newline at end of file From 86ac64ad42eb7de0f4fe15f06b52e93c34af3ef5 Mon Sep 17 00:00:00 2001 From: DyCTaTOR <125912249+DyCTaTOR@users.noreply.github.com> Date: Tue, 15 Oct 2024 10:29:10 +0400 Subject: [PATCH 2/2] =?UTF-8?q?lab1=20-=20=D1=81=D0=B4=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WinFormsLibrary1/WinFormsLibrary1/ListBox.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs b/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs index 715c98a..dc905e4 100644 --- a/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs +++ b/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs @@ -25,10 +25,12 @@ namespace WinFormsLibrary1 listBox.SelectedIndex = value; } } + public ListBoxUserControl() { InitializeComponent(); } + public void SetParams(string template, char? fromChar, char? toChar) { _template = template; @@ -49,7 +51,7 @@ namespace WinFormsLibrary1 var curObject = new T(); string text = listBox.SelectedItem?.ToString() ?? ""; - var words = System.Text.RegularExpressions.Regex.Split(_template, $@"\{_startSymbol.Value}.*?\{_endSymbol.Value}"); + var words = Regex.Split(_template, $@"\{_startSymbol.Value}.*?\{_endSymbol.Value}"); int firstWordStart = text.IndexOf(words[0], 0); if (firstWordStart == -1)