diff --git a/Components/Components/VisualComponents/CustomListBox.cs b/Components/Components/VisualComponents/CustomListBox.cs
index 5502c01..b71fd35 100644
--- a/Components/Components/VisualComponents/CustomListBox.cs
+++ b/Components/Components/VisualComponents/CustomListBox.cs
@@ -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
{
///
- /// Макетная строка
+ /// Конфигурация
///
- private string _layoutString;
-
- ///
- /// Символ начала имени свойства
- ///
- private string _startSymbol;
-
- ///
- /// Символ конца имени свойства
- ///
- private string _endSymbol;
+ private CustomListBoxConfig _config;
///
/// Индекс выбранной строки
@@ -37,11 +30,6 @@ namespace Components.VisualComponents
{
get
{
- if (listBox.SelectedIndex == -1)
- {
- return -1;
- }
-
return listBox.SelectedIndex;
}
@@ -63,25 +51,26 @@ namespace Components.VisualComponents
}
///
- /// Установить макетную строку
+ /// Задать конфигурацию
///
- ///
- ///
- ///
- public void SetLayoutInfo(string layoutString, string startSymbol, string endSymbol)
+ ///
+ ///
+ 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;
}
///
@@ -91,37 +80,28 @@ namespace Components.VisualComponents
///
public T? GetObject() 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);
+ }
}
}
diff --git a/Components/Components/VisualComponents/HelperModels/CustomListBoxConfig.cs b/Components/Components/VisualComponents/HelperModels/CustomListBoxConfig.cs
new file mode 100644
index 0000000..963c3b3
--- /dev/null
+++ b/Components/Components/VisualComponents/HelperModels/CustomListBoxConfig.cs
@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Components.VisualComponents.HelperModels
+{
+ ///
+ /// Вспомогательный класс конфигурации для CustomListBox
+ ///
+ public class CustomListBoxConfig
+ {
+ ///
+ /// Макетная строка
+ ///
+ public string LayoutString { get; set; } = string.Empty;
+
+ ///
+ /// Символ начала имени свойства
+ ///
+ public string StartSymbol { get; set; } = string.Empty;
+
+ ///
+ /// Символ конца имени свойства
+ ///
+ public string EndSymbol { get; set; } = string.Empty;
+
+ ///
+ /// Паттерн записи свойства
+ ///
+ public string Pattern => $"{StartSymbol}{"[\\w]+"}{EndSymbol}";
+ }
+}
diff --git a/Components/WinForms/FormVisualComponents.cs b/Components/WinForms/FormVisualComponents.cs
index 1d5c947..6ed06e9 100644
--- a/Components/WinForms/FormVisualComponents.cs
+++ b/Components/WinForms/FormVisualComponents.cs
@@ -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);
}
///