From da7b64ec6fb4743bbc4b206002a5820322699c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=91=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D1=81=D0=BA=D0=B0=D1=8F?= Date: Mon, 23 Sep 2024 01:06:36 +0400 Subject: [PATCH] =?UTF-8?q?=D1=87=D0=B5=D1=82=20=D0=BF=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D0=B8=D0=BB=D0=B0=20=D0=B2=20=D0=BB=D0=B0=D0=B1?= =?UTF-8?q?=D0=B5,=20=D0=BF=D0=BE=D0=B4=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20(=D0=B2=20=D1=82=D1=80=D0=B5=D1=82=D1=8C=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82?= =?UTF-8?q?=D0=B5...)=20=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Form1.Designer.cs | 1 + .../TestAppForCheckComponentsWorking/Form1.cs | 13 +++++- .../ListComponent.Designer.cs | 1 + .../UserComponentsOption19/ListComponent.cs | 44 ++++++++++++------- 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs index 07668f8..aaae1b1 100644 --- a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs +++ b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs @@ -56,6 +56,7 @@ listComponent.SelectedIndex = -1; listComponent.Size = new Size(406, 315); listComponent.TabIndex = 2; + listComponent.ChangeSelectedItem += ChangeSelectedItem; // // FormTest // diff --git a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs index 3c7245d..8082efc 100644 --- a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs +++ b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs @@ -43,6 +43,7 @@ namespace TestAppForCheckComponentsWorking public class Cat() { public string? Name { get; set; } + public int? Age { get; set; } } public void FillListBox() @@ -51,9 +52,10 @@ namespace TestAppForCheckComponentsWorking { Cat cat = new Cat(); cat.Name = i.ToString(); + cat.Age = i; try { - listComponent.FillTemplateString(" (Name)", "(", ")"); + listComponent.FillTemplateString(" (Name)", "(", ")"); } catch (Exception ex) { @@ -62,6 +64,15 @@ namespace TestAppForCheckComponentsWorking listComponent.AddObjectToListBox(cat); } } + + private void ChangeSelectedItem(object sender, EventArgs e) + { + listComponent.GetObjectFromSelectedRow(); + string placeHolder = "(Name)"; + listComponent.ExtractValueFromTemplate(" (Name)", placeHolder); + + } + } } diff --git a/UserComponentsOption19/UserComponentsOption19/ListComponent.Designer.cs b/UserComponentsOption19/UserComponentsOption19/ListComponent.Designer.cs index e06f21f..0255fe1 100644 --- a/UserComponentsOption19/UserComponentsOption19/ListComponent.Designer.cs +++ b/UserComponentsOption19/UserComponentsOption19/ListComponent.Designer.cs @@ -39,6 +39,7 @@ listBox.Name = "listBox"; listBox.Size = new Size(319, 244); listBox.TabIndex = 0; + listBox.SelectedIndexChanged += listBox_SelectedIndexChanged; // // ListComponent // diff --git a/UserComponentsOption19/UserComponentsOption19/ListComponent.cs b/UserComponentsOption19/UserComponentsOption19/ListComponent.cs index aa8d766..a6f19e6 100644 --- a/UserComponentsOption19/UserComponentsOption19/ListComponent.cs +++ b/UserComponentsOption19/UserComponentsOption19/ListComponent.cs @@ -17,9 +17,12 @@ namespace UserComponentsOption19 private string? _startPropertyChar; private string? _endPropertyChar; + public event EventHandler? ChangeSelectedItem; + public ListComponent() { InitializeComponent(); + //listBox.SelectedIndexChanged += listBox_SelectedIndexChanged; } /// @@ -66,12 +69,14 @@ namespace UserComponentsOption19 } } - public T? GetObjectFromSelectedRow() where T : new() + public T? GetObjectFromSelectedRow() where T : class, new() { - if (listBox.SelectedItem == null) - return default; + if (listBox.SelectedIndex == -1 || listBox.SelectedItem == null) + return null; string? selectedString = listBox.SelectedItem.ToString(); + //MessageBox.Show(selectedString); + T obj = new T(); // Получаем тип объекта @@ -85,38 +90,41 @@ namespace UserComponentsOption19 { // Извлекаем значение свойства из строки string value = ExtractValueFromTemplate(selectedString, placeholder); + MessageBox.Show(value.ToString()); // Преобразуем значение в нужный тип object convertedValue = Convert.ChangeType(value, property.PropertyType); + MessageBox.Show(convertedValue.ToString()); // Устанавливаем значение свойства через рефлексию property.SetValue(obj, convertedValue); } } return obj; + } - // Метод для извлечения значения между плейсхолдерами - private string ExtractValueFromTemplate(string templateString, string placeholder) - { - int startIndex = templateString.IndexOf(placeholder) + placeholder.Length; - int endIndex = templateString.IndexOf(_endPropertyChar, startIndex); + // имя кота: (Барсик) - if (endIndex == -1) endIndex = templateString.Length; + // Метод для извлечения значения между плейсхолдерами + public string ExtractValueFromTemplate(string templateString, string placeholder) + { + int startIndex = templateString.IndexOf(placeholder) + 1; + int endIndex = templateString.IndexOf(_endPropertyChar); + + if (endIndex < 0) endIndex = templateString.Length; // ЛУЧШЕ СДЕЛАТЬ ИСКЛЮЧЕНИЕ + + MessageBox.Show(templateString.Substring(startIndex, endIndex - startIndex).Trim()); return templateString.Substring(startIndex, endIndex - startIndex).Trim(); } - // Параметризованный метод для добавления объекта в ListBox + // Параметризованный метод для добавления объекта в листбокс public void AddObjectToListBox(T? obj) { - // Получаем тип объекта Type objectType = obj.GetType(); - - // Создаем строку на основе макетной строки string resultString = _templateString; - // Проходим по всем свойствам объекта foreach (var propertyInfo in objectType.GetProperties()) { string placeholder = $"{_startPropertyChar}{propertyInfo.Name}{_endPropertyChar}"; @@ -125,10 +133,12 @@ namespace UserComponentsOption19 // Заменяем плейсхолдеры значениями из объекта resultString = resultString.Replace(placeholder, value); } - - // Добавляем полученную строку в ListBox - listBox.Items.Add(resultString); + listBox.Items.Add(resultString); } + private void listBox_SelectedIndexChanged(object sender, EventArgs e) + { + ChangeSelectedItem?.Invoke(this, e); + } } }