2024-09-06 10:38:12 +04:00
|
|
|
|
using Controls.Exceptions;
|
|
|
|
|
using System;
|
2024-09-05 23:08:08 +04:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 22:57:47 +04:00
|
|
|
|
private string template;
|
|
|
|
|
private string start;
|
|
|
|
|
private string end;
|
2024-09-05 23:08:08 +04:00
|
|
|
|
|
2024-10-22 22:57:47 +04:00
|
|
|
|
public void setTemplate(string _template, string _start, string _end)
|
2024-09-05 23:08:08 +04:00
|
|
|
|
{
|
2024-10-22 22:57:47 +04:00
|
|
|
|
if (string.IsNullOrEmpty(_template) || string.IsNullOrEmpty(_start) || string.IsNullOrEmpty(_end))
|
2024-09-06 10:38:12 +04:00
|
|
|
|
{
|
2024-10-22 22:57:47 +04:00
|
|
|
|
throw new ArgumentNullException("Wrong template");
|
2024-09-06 10:38:12 +04:00
|
|
|
|
}
|
2024-10-22 22:57:47 +04:00
|
|
|
|
template = _template;
|
|
|
|
|
start = _start;
|
|
|
|
|
end = _end;
|
2024-09-05 23:08:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 22:57:47 +04:00
|
|
|
|
public int SelectedRow
|
2024-09-05 23:08:08 +04:00
|
|
|
|
{
|
2024-10-22 22:57:47 +04:00
|
|
|
|
get
|
2024-09-05 23:08:08 +04:00
|
|
|
|
{
|
2024-10-22 22:57:47 +04:00
|
|
|
|
return listBoxMain.SelectedIndex;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value < 0) return;
|
|
|
|
|
listBoxMain.SelectedIndex = value;
|
2024-09-05 23:08:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetObjectFromStr<T>() where T : class, new()
|
|
|
|
|
{
|
2024-10-22 22:57:47 +04:00
|
|
|
|
|
|
|
|
|
if (listBoxMain.SelectedIndex < 0)
|
2024-09-05 23:08:08 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-09-06 00:43:56 +04:00
|
|
|
|
string row = listBoxMain.SelectedItem.ToString();
|
2024-09-05 23:08:08 +04:00
|
|
|
|
T curObject = new T();
|
|
|
|
|
StringBuilder sb = new StringBuilder(row);
|
2024-10-22 22:57:47 +04:00
|
|
|
|
|
|
|
|
|
//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;
|
|
|
|
|
|
2024-09-05 23:08:08 +04:00
|
|
|
|
foreach (var property in typeof(T).GetProperties())
|
|
|
|
|
{
|
|
|
|
|
if (!property.CanWrite)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 22:57:47 +04:00
|
|
|
|
//MessageBox.Show(property.Name);
|
|
|
|
|
|
|
|
|
|
int startBorder = sb.ToString().IndexOf(start);
|
|
|
|
|
if (startBorder == -1)
|
2024-09-05 23:08:08 +04:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 22:57:47 +04:00
|
|
|
|
int endBorder = sb.ToString().IndexOf(end, startBorder + 1);
|
|
|
|
|
if (endBorder == -1)
|
2024-09-05 23:08:08 +04:00
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 22:57:47 +04:00
|
|
|
|
string propertyValue = sb.ToString(startBorder + 1, endBorder - startBorder - 1);
|
|
|
|
|
sb.Remove(0, endBorder + 1);
|
2024-09-05 23:08:08 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-10-22 22:57:47 +04:00
|
|
|
|
listBoxMain.Items.Add(template);
|
2024-09-05 23:08:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string row = listBoxMain.Items[rowIndex].ToString();
|
|
|
|
|
PropertyInfo propertyInfo = dataObject.GetType().GetProperty(propertyName);
|
|
|
|
|
|
|
|
|
|
if (propertyInfo != null)
|
|
|
|
|
{
|
2024-10-22 22:57:47 +04:00
|
|
|
|
var propertyValue = propertyInfo.GetValue(dataObject);
|
|
|
|
|
row = row.Replace($"{start}{propertyName}{end}", propertyValue.ToString());
|
2024-09-05 23:08:08 +04:00
|
|
|
|
listBoxMain.Items[rowIndex] = row;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-22 22:57:47 +04:00
|
|
|
|
|
|
|
|
|
public int CountRows()
|
|
|
|
|
{
|
|
|
|
|
return listBoxMain.Items.Count;
|
|
|
|
|
}
|
2024-09-05 23:08:08 +04:00
|
|
|
|
}
|
|
|
|
|
}
|