diff --git a/ComponentsLibrary/ListBoxValues.cs b/ComponentsLibrary/ListBoxValues.cs index 9013e81..8d4a90b 100644 --- a/ComponentsLibrary/ListBoxValues.cs +++ b/ComponentsLibrary/ListBoxValues.cs @@ -64,7 +64,7 @@ namespace ComponentsLibrary foreach (var prop in properties) { string propertyValue = prop.GetValue(item)?.ToString() ?? string.Empty; - text = text.Replace("{" + prop.Name + "}", propertyValue); + text = text.Replace($"{startSymbol}{prop.Name}{endSymbol}", propertyValue); } return text; @@ -73,74 +73,46 @@ namespace ComponentsLibrary // Публичный параметризованный метод для получения объекта из выбранной строки public T? GetSelectedItem() where T : new() { - var item = listBox.SelectedItem; - if (item != null) + var selectedItem = listBox.SelectedItem; + if (selectedItem != null) { + string selectedString = selectedItem.ToString(); // Строка из ListBox + T obj = new T(); PropertyInfo[] properties = typeof(T).GetProperties(); - string layout = layoutString; - string pattern = @"^(.*?){T}.*?(.*?){P}"; - //string input2 = "Температура воздуха ТЕМПЕРАТУРА ВЫСОКАЯ, давление НИЗКОЕ"; - string? selectedString = item?.ToString(); + string pattern = layoutString; - if (selectedString != null) + + for (int i = 0; i < properties.Length; i++) { - T obj = new T(); - - Regex regex = new Regex(pattern); - Match match = regex.Match(layout); - - string[] staticText = new string[properties.Length]; - - if (match.Success) - { - // Извлекаем текст до {T} и до {P} - //string beforeT = match.Groups[1].Value; - //string beforeP = match.Groups[2].Value; - for (int i = 0; i < staticText.Length; i++) - { - staticText[i] = match.Groups[i+1].Value; - } - - //string pattern2 = $@"{beforeT}(.*?)\{beforeP}(.*)\{beforeEnd}"; - //"Температура воздуха (.*?)\\, давление (.*)\\, такие дела" - string patternStatic = $@""; - for (int i = 0; i < staticText.Length; i++) - { - - if (i == staticText.Length - 1) - { - patternStatic += $"{staticText[i]}(.*)"; - } - else { - patternStatic += $"{staticText[i]}(.*?)\\"; - } - - } - - Regex regexValues = new Regex(patternStatic); - Match matchValues = regexValues.Match(selectedString); - - if (matchValues.Success) - { - //object val1 = matchValues.Groups[1].Value; - //object val2 = matchValues.Groups[2].Value; - - for (int i = 0; i < properties.Length; i++) - { - object value = Convert.ChangeType(matchValues.Groups[i+1].Value, properties[i].PropertyType); - properties[i].SetValue(obj, value); - } - return obj; - } + PropertyInfo prop = properties[i]; + string propertyPattern = $"{startSymbol}{prop.Name}{endSymbol}"; + if (i == properties.Length - 1) + { + pattern = pattern.Replace(propertyPattern, "(.*)"); + } + else + { + pattern = pattern.Replace(propertyPattern, "(.*?)"); } } - } + Regex regex = new Regex(pattern); + Match match = regex.Match(selectedString); + if (match.Success) + { + for (int i = 0; i < properties.Length; i++) + { + string value = match.Groups[i + 1].Value; + object convertedValue = Convert.ChangeType(value, properties[i].PropertyType); + properties[i].SetValue(obj, convertedValue); + } + return obj; + } + } return default; } - } } diff --git a/ComponentsView/FormComponents.cs b/ComponentsView/FormComponents.cs index 70e7097..6189b70 100644 --- a/ComponentsView/FormComponents.cs +++ b/ComponentsView/FormComponents.cs @@ -60,26 +60,23 @@ namespace ComponentsLab private void listBoxValues_Load(object sender, EventArgs e) { - listBoxValues.SetLayout(" {T}, {P}", '{', '}'); + listBoxValues.SetLayout(" [Temp], [Pressure]", '[', ']'); var objectList = new List { - new ObjectClass { T = " ", P = "1008" }, - new ObjectClass { T = "2", P = "1008" }, - new ObjectClass { T = "3", P = 1010 }, - new ObjectClass { T = "4", P = 1011 }, - new ObjectClass { T = "5", P = 1009 }, + new ObjectClass { Temp = " ", Pressure = "1008" }, + new ObjectClass { Temp = "2", Pressure = "1008" }, + new ObjectClass { Temp = "3", Pressure = 1010 }, + new ObjectClass { Temp = "4", Pressure = 1011 }, + new ObjectClass { Temp = "5", Pressure = 1009 }, }; listBoxValues.FillListBox(objectList); } private void listBoxValues_GetObject(object sender, EventArgs e) { - try { var selectedItem = listBoxValues.GetSelectedItem(); - checkParametrs(); - - + checkParametrs(); } catch (EmailException ex) { @@ -90,10 +87,9 @@ namespace ComponentsLab private void checkParametrs() { ObjectClass obj = listBoxValues.GetSelectedItem(); - object? T = obj?.T; - object? P = obj?.P; + object? T = obj?.Temp; + object? P = obj?.Pressure; MessageBox.Show($" , : {T}, : {P}"); - } } } diff --git a/ComponentsView/ObjectClass.cs b/ComponentsView/ObjectClass.cs index fda6eb7..1a85429 100644 --- a/ComponentsView/ObjectClass.cs +++ b/ComponentsView/ObjectClass.cs @@ -2,7 +2,7 @@ { public class ObjectClass { - public object T { get; set; } - public object P { get; set; } + public object? Temp { get; set; } + public object? Pressure { get; set; } } }