151 lines
4.6 KiB
C#
151 lines
4.6 KiB
C#
using Controls.Exceptions;
|
|
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.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Controls
|
|
{
|
|
public partial class CustomListBox : UserControl
|
|
{
|
|
/// <summary>
|
|
/// Конструктор по умолчанию
|
|
/// </summary>
|
|
public CustomListBox()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private string template;
|
|
private string start;
|
|
private string end;
|
|
|
|
public void setTemplate(string _template, string _start, string _end)
|
|
{
|
|
if (string.IsNullOrEmpty(_template) || string.IsNullOrEmpty(_start) || string.IsNullOrEmpty(_end))
|
|
{
|
|
throw new ArgumentNullException("Wrong template");
|
|
}
|
|
template = _template;
|
|
start = _start;
|
|
end = _end;
|
|
}
|
|
|
|
public int SelectedRow
|
|
{
|
|
get
|
|
{
|
|
return listBoxMain.SelectedIndex;
|
|
}
|
|
set
|
|
{
|
|
if (value < 0) return;
|
|
listBoxMain.SelectedIndex = value;
|
|
}
|
|
}
|
|
|
|
public T GetObjectFromStr<T>() where T : class, new()
|
|
{
|
|
|
|
if (listBoxMain.SelectedIndex < 0)
|
|
{
|
|
return null;
|
|
}
|
|
string row = listBoxMain.SelectedItem.ToString();
|
|
T curObject = new T();
|
|
StringBuilder sb = new StringBuilder(row);
|
|
|
|
//MessageBox.Show(sb.ToString());
|
|
|
|
string[] words = template.Split(new[] { char.Parse(start), char.Parse(end) });
|
|
|
|
// Дорогой дневник, мне не подобрать слов чтобы описать всю {Mood}, что я испытал сегодня; {Date}
|
|
// Дорогой дневник, мне не подобрать слов чтобы описать всю радость, что я испытал сегодня; 01.01.01
|
|
|
|
StringBuilder myrow = new StringBuilder(row);
|
|
List<string> flexPartsTemplate = new();
|
|
foreach (string word in words)
|
|
{
|
|
if (row.Contains(word) && !string.IsNullOrEmpty(word))
|
|
{
|
|
myrow.Replace(word, end);
|
|
}
|
|
else
|
|
{
|
|
flexPartsTemplate.Add(word);
|
|
}
|
|
}
|
|
string[] flexParts = myrow.ToString().Split(end);
|
|
int i = 1;
|
|
StringBuilder result = new StringBuilder(template);
|
|
foreach (string word in flexPartsTemplate)
|
|
{
|
|
if (!string.IsNullOrEmpty(word))
|
|
{
|
|
result.Replace(word, flexParts[i]);
|
|
i++;
|
|
}
|
|
|
|
}
|
|
|
|
sb = result;
|
|
|
|
foreach (var property in typeof(T).GetProperties())
|
|
{
|
|
if (!property.CanWrite)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
//MessageBox.Show(property.Name);
|
|
|
|
int startBorder = sb.ToString().IndexOf(start);
|
|
if (startBorder == -1)
|
|
{
|
|
break;
|
|
}
|
|
|
|
int endBorder = sb.ToString().IndexOf(end, startBorder + 1);
|
|
if (endBorder == -1)
|
|
{
|
|
break;
|
|
}
|
|
|
|
string propertyValue = sb.ToString(startBorder + 1, endBorder - startBorder - 1);
|
|
sb.Remove(0, endBorder + 1);
|
|
property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType));
|
|
}
|
|
return curObject;
|
|
}
|
|
|
|
public void FillProperty<T>(T dataObject, int rowIndex, string propertyName)
|
|
{
|
|
while (listBoxMain.Items.Count <= rowIndex)
|
|
{
|
|
listBoxMain.Items.Add(template);
|
|
}
|
|
|
|
string row = listBoxMain.Items[rowIndex].ToString();
|
|
PropertyInfo propertyInfo = dataObject.GetType().GetProperty(propertyName);
|
|
|
|
if (propertyInfo != null)
|
|
{
|
|
var propertyValue = propertyInfo.GetValue(dataObject);
|
|
row = row.Replace($"{start}{propertyName}{end}", propertyValue.ToString());
|
|
listBoxMain.Items[rowIndex] = row;
|
|
}
|
|
}
|
|
|
|
public int CountRows()
|
|
{
|
|
return listBoxMain.Items.Count;
|
|
}
|
|
}
|
|
}
|