From f098199010563f058d157be32904f301d522ff83 Mon Sep 17 00:00:00 2001 From: revengel66 Date: Mon, 30 Sep 2024 20:39:14 +0400 Subject: [PATCH] ready lab1 --- ComponentsLibrary/ListBoxValues.Designer.cs | 1 + ComponentsLibrary/ListBoxValues.cs | 71 ++++++++++++++------- ComponentsView/FormComponents.Designer.cs | 15 ++++- ComponentsView/FormComponents.cs | 13 ++++ 4 files changed, 77 insertions(+), 23 deletions(-) diff --git a/ComponentsLibrary/ListBoxValues.Designer.cs b/ComponentsLibrary/ListBoxValues.Designer.cs index 3c67356..5a76a2a 100644 --- a/ComponentsLibrary/ListBoxValues.Designer.cs +++ b/ComponentsLibrary/ListBoxValues.Designer.cs @@ -40,6 +40,7 @@ listBox.Name = "listBox"; listBox.Size = new Size(222, 124); listBox.TabIndex = 0; + listBox.SelectedIndexChanged += listBox_SelectedIndexChanged; // // ListBoxValues // diff --git a/ComponentsLibrary/ListBoxValues.cs b/ComponentsLibrary/ListBoxValues.cs index 2588f10..b7f44a0 100644 --- a/ComponentsLibrary/ListBoxValues.cs +++ b/ComponentsLibrary/ListBoxValues.cs @@ -5,16 +5,27 @@ namespace ComponentsLibrary { public partial class ListBoxValues : UserControl { - //private List _employees; private string layoutString = string.Empty; private char startSymbol = '{'; private char endSymbol = '}'; private List items = new List(); + private EventHandler? _getObject; public ListBoxValues() { InitializeComponent(); } + //событие, вызываемое при выборе строки + public event EventHandler GetObject + { + add { _getObject += value; } + remove { _getObject -= value; } + } + private void listBox_SelectedIndexChanged(object sender, EventArgs e) + { + _getObject?.Invoke(this, e); + } + //Публичное свойство для установки и получения индекса выбранной строки (set, get). public int SelectedIndex { @@ -36,35 +47,51 @@ namespace ComponentsLibrary items.Clear(); foreach (var item in itemList) { - items.Add(item); - string displayText = CreateDisplayText(item); - listBox.Items.Add(displayText); + if (item != null) { + items.Add(item); + string displayText = CreateDisplayText(item); + listBox.Items.Add(displayText); + } } } // Метод для создания строки на основе макета private string CreateDisplayText(object item) { string text = layoutString; - // Регулярное выражение для поиска имен свойств - var regex = new Regex($@"{startSymbol}(\w+){endSymbol}"); - return regex.Replace(text, match => - { - string propertyName = match.Groups[1].Value; - PropertyInfo prop = item.GetType().GetProperty(propertyName); - if (prop != null) - { - return prop.GetValue(item)?.ToString() ?? string.Empty; - } - return match.Value; // Возвращаем то, что нашли, если свойства нет - }); - } + PropertyInfo[] properties = item.GetType().GetProperties(); - // Публичный параметризованный метод для получения объекта из выбранной строки - public T GetSelectedItem() - { - if (listBox.SelectedIndex != -1) + foreach (var prop in properties) { - return (T)items[listBox.SelectedIndex]; + string propertyValue = prop.GetValue(item)?.ToString().Replace(',', '.') ?? string.Empty; + text = text.Replace("{" + prop.Name + "}", propertyValue); + } + + return text; + + } + // Публичный параметризованный метод для получения объекта из выбранной строки + public T? GetSelectedItem() where T : new() + { + var item = listBox.SelectedItem; + if (item != null) + { + // Получаем строковое значение + string? selectedString = item?.ToString(); + + if (selectedString != null) { + T obj = new T(); + + MatchCollection matches = Regex.Matches(selectedString, @"\d+(\,\d+)?"); + + PropertyInfo[] properties = typeof(T).GetProperties(); + + for (int i = 0; i < properties.Length && i < matches.Count; i++) + { + object value = Convert.ChangeType(matches[i].Value.Replace(",", "."), properties[i].PropertyType); + properties[i].SetValue(obj, value); + } + return obj; + } } return default; } diff --git a/ComponentsView/FormComponents.Designer.cs b/ComponentsView/FormComponents.Designer.cs index f9b018d..884deab 100644 --- a/ComponentsView/FormComponents.Designer.cs +++ b/ComponentsView/FormComponents.Designer.cs @@ -34,6 +34,7 @@ emailComponent = new ComponentsLibrary.Email(); buttonCheckEmail = new Button(); listBoxValues = new ComponentsLibrary.ListBoxValues(); + buttonGetListSelectedItem = new Button(); SuspendLayout(); // // imageLoad @@ -84,13 +85,24 @@ listBoxValues.SelectedIndex = -1; listBoxValues.Size = new Size(516, 224); listBoxValues.TabIndex = 4; + listBoxValues.GetObject += listBoxValues_GetObject; listBoxValues.Load += listBoxValues_Load; // + // buttonGetListSelectedItem + // + buttonGetListSelectedItem.Location = new Point(12, 341); + buttonGetListSelectedItem.Name = "buttonGetListSelectedItem"; + buttonGetListSelectedItem.Size = new Size(133, 23); + buttonGetListSelectedItem.TabIndex = 5; + buttonGetListSelectedItem.Text = "Получить объект"; + buttonGetListSelectedItem.UseVisualStyleBackColor = true; + // // FormComponents // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(549, 345); + ClientSize = new Size(551, 376); + Controls.Add(buttonGetListSelectedItem); Controls.Add(listBoxValues); Controls.Add(buttonCheckEmail); Controls.Add(emailComponent); @@ -109,5 +121,6 @@ private ComponentsLibrary.Email emailComponent; private Button buttonCheckEmail; private ComponentsLibrary.ListBoxValues listBoxValues; + private Button buttonGetListSelectedItem; } } diff --git a/ComponentsView/FormComponents.cs b/ComponentsView/FormComponents.cs index f979b86..4102bd4 100644 --- a/ComponentsView/FormComponents.cs +++ b/ComponentsView/FormComponents.cs @@ -71,5 +71,18 @@ namespace ComponentsLab }; listBoxValues.FillListBox(objectList); } + private void listBoxValues_GetObject(object sender, EventArgs e) + { + + try + { + var selectedItem = listBoxValues.GetSelectedItem(); + MessageBox.Show($" : {selectedItem}"); + } + catch (EmailException ex) + { + MessageBox.Show(ex.Message, $" ", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } } }