2024-09-16 20:52:42 +04:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2024-10-01 15:35:05 +04:00
|
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
2024-09-16 20:52:42 +04:00
|
|
|
|
|
|
|
|
|
namespace ComponentsLibrary
|
|
|
|
|
{
|
|
|
|
|
public partial class ListBoxValues : UserControl
|
|
|
|
|
{
|
|
|
|
|
private string layoutString = string.Empty;
|
|
|
|
|
private char startSymbol = '{';
|
|
|
|
|
private char endSymbol = '}';
|
|
|
|
|
private List<object> items = new List<object>();
|
2024-09-30 20:39:14 +04:00
|
|
|
|
private EventHandler? _getObject;
|
2024-09-16 20:52:42 +04:00
|
|
|
|
public ListBoxValues()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 20:39:14 +04:00
|
|
|
|
//событие, вызываемое при выборе строки
|
|
|
|
|
public event EventHandler GetObject
|
|
|
|
|
{
|
|
|
|
|
add { _getObject += value; }
|
|
|
|
|
remove { _getObject -= value; }
|
|
|
|
|
}
|
|
|
|
|
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_getObject?.Invoke(this, e);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 20:52:42 +04:00
|
|
|
|
//Публичное свойство для установки и получения индекса выбранной строки (set, get).
|
|
|
|
|
public int SelectedIndex
|
|
|
|
|
{
|
|
|
|
|
get { return listBox.SelectedIndex; }
|
|
|
|
|
set { listBox.SelectedIndex = value; }
|
|
|
|
|
}
|
|
|
|
|
// Публичный метод для установки макетной строки и символов
|
|
|
|
|
public void SetLayout(string layout, char startChar, char endChar)
|
|
|
|
|
{
|
|
|
|
|
layoutString = layout;
|
|
|
|
|
startSymbol = startChar;
|
|
|
|
|
endSymbol = endChar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Публичный метод для заполнения ListBox
|
|
|
|
|
public void FillListBox<T>(IEnumerable<T> itemList)
|
|
|
|
|
{
|
|
|
|
|
listBox.Items.Clear();
|
|
|
|
|
items.Clear();
|
|
|
|
|
foreach (var item in itemList)
|
|
|
|
|
{
|
2024-09-30 20:39:14 +04:00
|
|
|
|
if (item != null) {
|
|
|
|
|
items.Add(item);
|
|
|
|
|
string displayText = CreateDisplayText(item);
|
|
|
|
|
listBox.Items.Add(displayText);
|
|
|
|
|
}
|
2024-09-16 20:52:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Метод для создания строки на основе макета
|
|
|
|
|
private string CreateDisplayText(object item)
|
|
|
|
|
{
|
|
|
|
|
string text = layoutString;
|
2024-09-30 20:39:14 +04:00
|
|
|
|
PropertyInfo[] properties = item.GetType().GetProperties();
|
|
|
|
|
|
|
|
|
|
foreach (var prop in properties)
|
2024-09-16 20:52:42 +04:00
|
|
|
|
{
|
2024-10-01 15:35:05 +04:00
|
|
|
|
string propertyValue = prop.GetValue(item)?.ToString() ?? string.Empty;
|
2024-10-10 17:57:14 +04:00
|
|
|
|
text = text.Replace($"{startSymbol}{prop.Name}{endSymbol}", propertyValue);
|
2024-09-30 20:39:14 +04:00
|
|
|
|
}
|
2024-09-16 20:52:42 +04:00
|
|
|
|
|
2024-09-30 20:39:14 +04:00
|
|
|
|
return text;
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-01 15:35:05 +04:00
|
|
|
|
// Публичный параметризованный метод для получения объекта из выбранной строки
|
2024-10-15 10:44:45 +04:00
|
|
|
|
public T GetSelectedItem<T>() where T : new()
|
2024-10-01 15:35:05 +04:00
|
|
|
|
{
|
2024-10-10 17:57:14 +04:00
|
|
|
|
var selectedItem = listBox.SelectedItem;
|
2024-10-15 10:44:45 +04:00
|
|
|
|
if (selectedItem == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception();
|
|
|
|
|
}
|
|
|
|
|
string selectedString = selectedItem.ToString();
|
|
|
|
|
T obj = new T();
|
2024-10-01 15:35:05 +04:00
|
|
|
|
|
2024-10-15 10:44:45 +04:00
|
|
|
|
PropertyInfo[] properties = typeof(T).GetProperties();
|
2024-10-01 15:39:15 +04:00
|
|
|
|
|
2024-10-15 10:44:45 +04:00
|
|
|
|
string pattern = layoutString;
|
2024-10-01 15:35:05 +04:00
|
|
|
|
|
2024-10-10 17:57:14 +04:00
|
|
|
|
|
2024-10-15 10:44:45 +04:00
|
|
|
|
for (int i = 0; i < properties.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
PropertyInfo prop = properties[i];
|
|
|
|
|
string propertyPattern = $"{startSymbol}{prop.Name}{endSymbol}";
|
|
|
|
|
if (i == properties.Length - 1)
|
|
|
|
|
{
|
|
|
|
|
pattern = pattern.Replace(propertyPattern, "(.*)");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pattern = pattern.Replace(propertyPattern, "(.*?)");
|
2024-10-10 17:57:14 +04:00
|
|
|
|
}
|
2024-10-15 10:44:45 +04:00
|
|
|
|
}
|
|
|
|
|
Regex regex = new Regex(pattern);
|
|
|
|
|
Match match = regex.Match(selectedString);
|
2024-10-01 15:35:05 +04:00
|
|
|
|
|
2024-10-15 10:44:45 +04:00
|
|
|
|
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);
|
2024-10-01 15:35:05 +04:00
|
|
|
|
}
|
2024-10-15 10:44:45 +04:00
|
|
|
|
return obj;
|
2024-10-01 15:35:05 +04:00
|
|
|
|
|
2024-10-15 10:44:45 +04:00
|
|
|
|
}
|
2024-10-01 15:35:05 +04:00
|
|
|
|
}
|
2024-09-16 20:52:42 +04:00
|
|
|
|
}
|