From 0b05d77cdde924c6a9d06a0a5ee309ecf9c36571 Mon Sep 17 00:00:00 2001 From: Artyom_Yashin Date: Wed, 25 Sep 2024 10:35:18 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=B4=D0=B0=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KOP_Labs/Library_var_4_lab_1/CustomListBox.cs | 64 ++++++++++++------- KOP_Labs/Library_var_4_lab_1/DropDownList.cs | 15 +---- KOP_Labs/TestProj/Human.cs | 2 +- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/KOP_Labs/Library_var_4_lab_1/CustomListBox.cs b/KOP_Labs/Library_var_4_lab_1/CustomListBox.cs index 6e0a056..9815402 100644 --- a/KOP_Labs/Library_var_4_lab_1/CustomListBox.cs +++ b/KOP_Labs/Library_var_4_lab_1/CustomListBox.cs @@ -15,6 +15,8 @@ namespace Library_var_4_lab_1 public partial class CustomListBox : UserControl { private List properties = new(); + string StartSimbol = string.Empty; + string EndSimbol = string.Empty; private List> values = new List>(); private string Template = string.Empty; private Regex Regex = new(string.Empty); @@ -24,10 +26,12 @@ namespace Library_var_4_lab_1 InitializeComponent(); } - public void SetTemplate(string StartSimbol, string EndSimbol, string template) + public void SetTemplate(string startSimbol, string endSimbol, string template) { - string pattern = "\\" + StartSimbol + "([^"+EndSimbol+"]*)\\" + EndSimbol; + string pattern = "\\" + startSimbol + "([^"+endSimbol+"]*)\\" + endSimbol; Template = template; + StartSimbol = startSimbol; + EndSimbol = endSimbol; Regex = new Regex(pattern); properties.Clear(); listBox.Items.Clear(); @@ -54,34 +58,50 @@ namespace Library_var_4_lab_1 public void Add(T Object) { - List tmpList = new(); - tmpList.AddRange(properties); - string[] splitStr = Regex.Split(Template); - string resultString = string.Empty; - for(int i = 0; i < splitStr.Length-1; i += 2) + var tmpTemplate = Template; + int i = 0; + foreach (var property in properties) { - resultString = string.Concat(resultString, splitStr[i]); - if (tmpList.Count > 0) - { - var tmpValue = typeof(T)?.GetProperty(tmpList.First())?.GetValue(Object); - resultString = string.Concat(resultString, tmpValue?.ToString() ?? string.Empty); - values[properties.Count - tmpList.Count].Add(tmpValue ?? string.Empty); - tmpList.RemoveAt(0); - } + var tmpValue = typeof(T)?.GetProperty(property)?.GetValue(Object); + tmpTemplate = tmpTemplate.Replace(StartSimbol + property + EndSimbol, tmpValue?.ToString() ?? string.Empty); + values[i].Add(tmpValue ?? string.Empty); + i++; + } - listBox.Items.Add(resultString); + listBox.Items.Add(tmpTemplate); } public T GetSelected() where T : class, new() { - Type objectType = typeof(T); - T Object = new T(); - for (int i = 0; i < properties.Count; i++) + string SelectedStr = ""; + if (listBox.SelectedIndex != -1) { - var field = objectType.GetProperty(properties[i]); - field?.SetValue(Object, values[i][listBox.SelectedIndex]); + SelectedStr = listBox.SelectedItem.ToString(); } - return Object; + + T currentObject = new T(); + Type objectType = typeof(T); + int rowIndex = listBox.SelectedIndex; + int colIndex = 0; + foreach (var prop in properties) + { + var field = objectType.GetProperty(prop); + if (!field?.CanWrite ?? false) + { + continue; + } + int startS = SelectedStr.IndexOf(values[colIndex][rowIndex].ToString()); + if (startS == -1) + { + break; + } + string propValue = SelectedStr.Substring(startS, values[colIndex][rowIndex].ToString().Length); + if (SelectedStr.Length > startS + values[colIndex][rowIndex].ToString().Length + 1) + SelectedStr = SelectedStr.Substring(startS + values[colIndex][rowIndex].ToString().Length + 1); + field?.SetValue(currentObject, Convert.ChangeType(propValue, field.PropertyType)); + colIndex++; + } + return currentObject; } } } diff --git a/KOP_Labs/Library_var_4_lab_1/DropDownList.cs b/KOP_Labs/Library_var_4_lab_1/DropDownList.cs index c79ba2c..81c0252 100644 --- a/KOP_Labs/Library_var_4_lab_1/DropDownList.cs +++ b/KOP_Labs/Library_var_4_lab_1/DropDownList.cs @@ -27,19 +27,8 @@ namespace Library_var_4_lab_1 public string SelectedItem { - get => comboBox.SelectedIndex != -1 ? comboBox.SelectedItem.ToString() ?? string.Empty : string.Empty; - set - { - if (comboBox.Items.Contains(value)) - { - comboBox.SelectedItem = value; - } - else - { - comboBox.SelectedIndex = -1; - } - - } + get => comboBox.SelectedItem?.ToString() ?? string.Empty; + set => comboBox.SelectedIndex = comboBox.Items.IndexOf(value); } public void Input(string value) diff --git a/KOP_Labs/TestProj/Human.cs b/KOP_Labs/TestProj/Human.cs index d93a48a..2793ac1 100644 --- a/KOP_Labs/TestProj/Human.cs +++ b/KOP_Labs/TestProj/Human.cs @@ -10,6 +10,6 @@ namespace TestProj { public string Name { get; set; } = string.Empty; public string SurName { get; set; } = string.Empty; - public int? Age { get; set; } + public int Age { get; set; } } }