CustomListBox Fix
This commit is contained in:
parent
df692293b8
commit
58e1420ff8
@ -1,10 +1,13 @@
|
||||
using System;
|
||||
using Components.VisualComponents.HelperModels;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
@ -16,19 +19,9 @@ namespace Components.VisualComponents
|
||||
public partial class CustomListBox : UserControl
|
||||
{
|
||||
/// <summary>
|
||||
/// Макетная строка
|
||||
/// Конфигурация
|
||||
/// </summary>
|
||||
private string _layoutString;
|
||||
|
||||
/// <summary>
|
||||
/// Символ начала имени свойства
|
||||
/// </summary>
|
||||
private string _startSymbol;
|
||||
|
||||
/// <summary>
|
||||
/// Символ конца имени свойства
|
||||
/// </summary>
|
||||
private string _endSymbol;
|
||||
private CustomListBoxConfig _config;
|
||||
|
||||
/// <summary>
|
||||
/// Индекс выбранной строки
|
||||
@ -37,11 +30,6 @@ namespace Components.VisualComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (listBox.SelectedIndex == -1)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return listBox.SelectedIndex;
|
||||
}
|
||||
|
||||
@ -63,25 +51,26 @@ namespace Components.VisualComponents
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Установить макетную строку
|
||||
/// Задать конфигурацию
|
||||
/// </summary>
|
||||
/// <param name="layoutString"></param>
|
||||
/// <param name="startSymbol"></param>
|
||||
/// <param name="endSymbol"></param>
|
||||
public void SetLayoutInfo(string layoutString, string startSymbol, string endSymbol)
|
||||
/// <param name="config"></param>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public void SetConfig(CustomListBoxConfig config)
|
||||
{
|
||||
if (string.IsNullOrEmpty(layoutString) || string.IsNullOrEmpty(startSymbol) || string.IsNullOrEmpty(endSymbol))
|
||||
if (_config != null)
|
||||
{
|
||||
throw new Exception("Конфигурация уже задана!");
|
||||
}
|
||||
if (string.IsNullOrEmpty(config.LayoutString) || string.IsNullOrEmpty(config.StartSymbol) || string.IsNullOrEmpty(config.EndSymbol))
|
||||
{
|
||||
throw new Exception("Заполните макетную строку!");
|
||||
}
|
||||
if (!layoutString.Contains(startSymbol) || !layoutString.Contains(endSymbol))
|
||||
if (!config.LayoutString.Contains(config.StartSymbol) || !config.LayoutString.Contains(config.EndSymbol))
|
||||
{
|
||||
throw new Exception("Макетная строка не содержит нужные элементы!");
|
||||
}
|
||||
|
||||
_layoutString = layoutString;
|
||||
_startSymbol = startSymbol;
|
||||
_endSymbol = endSymbol;
|
||||
|
||||
_config = config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -91,37 +80,28 @@ namespace Components.VisualComponents
|
||||
/// <returns></returns>
|
||||
public T? GetObject<T>() where T : class, new()
|
||||
{
|
||||
if (listBox.SelectedIndex == -1)
|
||||
if (listBox.SelectedIndex == -1 || _config == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string selectedString = listBox.SelectedItem.ToString()!;
|
||||
StringBuilder sb = new StringBuilder(selectedString);
|
||||
string layoutString = _config.LayoutString;
|
||||
T obj = new T();
|
||||
|
||||
foreach (var property in obj.GetType().GetProperties())
|
||||
|
||||
string[] layoutParts = Regex.Split(layoutString, _config.Pattern);
|
||||
for (int i = 0; i < layoutParts.Length - 1; i++)
|
||||
{
|
||||
if (!property.CanWrite)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
int startPosition;
|
||||
string matchedText = GetSubstring(layoutParts[i], layoutParts[i + 1], selectedString, out startPosition);
|
||||
selectedString = selectedString.Remove(0, startPosition + matchedText.Length);
|
||||
|
||||
int firstBorder = sb.ToString().IndexOf(_startSymbol);
|
||||
if (firstBorder == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
string patternSegment = GetSubstring(layoutParts[i], layoutParts[i + 1], layoutString, out startPosition);
|
||||
layoutString = layoutString.Remove(0, startPosition + patternSegment.Length);
|
||||
|
||||
int secondBorder = sb.ToString().IndexOf(_endSymbol, firstBorder + 1);
|
||||
if (secondBorder == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
string propertyValue = sb.ToString(firstBorder + 1, secondBorder - firstBorder - 1);
|
||||
sb.Remove(0, secondBorder + 1);
|
||||
property.SetValue(obj, Convert.ChangeType(propertyValue, property.PropertyType));
|
||||
patternSegment = patternSegment.Substring(1, patternSegment.Length - 2);
|
||||
PropertyInfo property = obj.GetType().GetProperty(patternSegment);
|
||||
property?.SetValue(obj, Convert.ChangeType(matchedText, property?.PropertyType));
|
||||
}
|
||||
|
||||
return obj;
|
||||
@ -140,23 +120,28 @@ namespace Components.VisualComponents
|
||||
{
|
||||
throw new ArgumentNullException("Добавляемый объект не существует!");
|
||||
}
|
||||
if (string.IsNullOrEmpty(_layoutString) || string.IsNullOrEmpty(_startSymbol) || string.IsNullOrEmpty(_endSymbol))
|
||||
if (_config == null)
|
||||
{
|
||||
throw new Exception("Заполните макетную строку!");
|
||||
}
|
||||
if (!_layoutString.Contains(_startSymbol) || !_layoutString.Contains(_endSymbol))
|
||||
{
|
||||
throw new Exception("Макетная строка не содержит нужные элементы!");
|
||||
throw new Exception("Конфигурация не задана!");
|
||||
}
|
||||
|
||||
string processedString = _layoutString;
|
||||
foreach (var property in obj.GetType().GetProperties())
|
||||
string processedString = _config.LayoutString;
|
||||
MatchCollection matchCollection = Regex.Matches(_config.LayoutString, _config.Pattern);
|
||||
foreach (Match item in matchCollection)
|
||||
{
|
||||
string placeholder = $"{_startSymbol}{property.Name}{_endSymbol}";
|
||||
processedString = processedString.Replace(placeholder, $"{_startSymbol}{property.GetValue(obj)}{_endSymbol}");
|
||||
string propertyName = item.Value.Remove(item.Value.Length - 1, 1).Remove(0, 1);
|
||||
processedString = processedString.Replace($"{_config.StartSymbol}{propertyName}{_config.EndSymbol}", obj.GetType().GetProperty(propertyName)?.GetValue(obj, null)?.ToString());
|
||||
}
|
||||
|
||||
listBox.Items.Add(processedString);
|
||||
}
|
||||
|
||||
private string GetSubstring(string startDelimiter, string endDelimiter, string baseString, out int startPosition)
|
||||
{
|
||||
startPosition = startDelimiter.Length;
|
||||
int endPosition = baseString.IndexOf(endDelimiter) - 1;
|
||||
endPosition = (endPosition >= startPosition) ? endPosition : (baseString.Length - 1);
|
||||
return baseString.Substring(startPosition, endPosition - startPosition + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Components.VisualComponents.HelperModels
|
||||
{
|
||||
/// <summary>
|
||||
/// Вспомогательный класс конфигурации для CustomListBox
|
||||
/// </summary>
|
||||
public class CustomListBoxConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// Макетная строка
|
||||
/// </summary>
|
||||
public string LayoutString { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Символ начала имени свойства
|
||||
/// </summary>
|
||||
public string StartSymbol { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Символ конца имени свойства
|
||||
/// </summary>
|
||||
public string EndSymbol { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Паттерн записи свойства
|
||||
/// </summary>
|
||||
public string Pattern => $"{StartSymbol}{"[\\w]+"}{EndSymbol}";
|
||||
}
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
using Components.VisualComponents.HelperModels;
|
||||
using WinForms.DataModels;
|
||||
|
||||
namespace WinForms
|
||||
@ -20,7 +21,11 @@ namespace WinForms
|
||||
|
||||
customTextBox.DatePattern = @"^(\d{2}.\d{2}.\d{4})$";
|
||||
|
||||
customListBox.SetLayoutInfo("Čě˙ *Name* Ôŕěčëč˙ *Surname*", "*", "*");
|
||||
CustomListBoxConfig config = new();
|
||||
config.LayoutString = "Èìÿ {Name} Ôàìèëèÿ {Surname}";
|
||||
config.StartSymbol = "{";
|
||||
config.EndSymbol = "}";
|
||||
customListBox.SetConfig(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Loading…
Reference in New Issue
Block a user