2024-09-16 20:52:42 +04:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
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-09-30 20:39:14 +04:00
|
|
|
|
string propertyValue = prop.GetValue(item)?.ToString().Replace(',', '.') ?? string.Empty;
|
|
|
|
|
text = text.Replace("{" + prop.Name + "}", propertyValue);
|
|
|
|
|
}
|
2024-09-16 20:52:42 +04:00
|
|
|
|
|
2024-09-30 20:39:14 +04:00
|
|
|
|
return text;
|
|
|
|
|
|
|
|
|
|
}
|
2024-09-16 20:52:42 +04:00
|
|
|
|
// Публичный параметризованный метод для получения объекта из выбранной строки
|
2024-09-30 20:39:14 +04:00
|
|
|
|
public T? GetSelectedItem<T>() where T : new()
|
2024-09-16 20:52:42 +04:00
|
|
|
|
{
|
2024-09-30 20:39:14 +04:00
|
|
|
|
var item = listBox.SelectedItem;
|
|
|
|
|
if (item != null)
|
2024-09-16 20:52:42 +04:00
|
|
|
|
{
|
2024-09-30 20:39:14 +04:00
|
|
|
|
// Получаем строковое значение
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-09-16 20:52:42 +04:00
|
|
|
|
}
|
|
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|