2024-09-20 03:20:29 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Text;
|
2024-10-02 19:19:16 +03:00
|
|
|
|
using System.Text.RegularExpressions;
|
2024-09-20 03:20:29 +03:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace Components
|
|
|
|
|
{
|
|
|
|
|
public partial class CustomListBox : UserControl
|
|
|
|
|
{
|
|
|
|
|
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 listBox.SelectedIndex;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value < 0) return;
|
|
|
|
|
listBox.SelectedIndex = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetObjectFromStr<T>() where T : class, new()
|
|
|
|
|
{
|
|
|
|
|
if (listBox.SelectedIndex < 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
string row = listBox.SelectedItem.ToString();
|
|
|
|
|
T curObject = new T();
|
|
|
|
|
StringBuilder sb = new StringBuilder(row);
|
2024-09-21 21:03:55 +03:00
|
|
|
|
|
|
|
|
|
MessageBox.Show(sb.ToString());
|
|
|
|
|
|
|
|
|
|
//("Дорогой дневник, мне не подобрать слов чтобы описать всю {Mood}, что я испытал сегодня; {Date}", "{", "}");
|
|
|
|
|
// "Дорогой дневник, мне не подобрать слов чтобы описать всю радость пенис, что я испытал сегодня; блаблабла"
|
|
|
|
|
StringBuilder tp = new StringBuilder(template);
|
|
|
|
|
|
2024-10-02 19:19:16 +03:00
|
|
|
|
string cleanTemplate = Regex.Replace(template, @"\{.*?\}", "");
|
|
|
|
|
List<char> symbolsTemplate = cleanTemplate.ToCharArray().ToList();
|
|
|
|
|
List<char> symbols = row.ToCharArray().ToList();
|
2024-09-21 21:03:55 +03:00
|
|
|
|
|
2024-10-02 19:19:16 +03:00
|
|
|
|
int iTemp = 0;
|
|
|
|
|
bool isTemplate = true;
|
|
|
|
|
bool islastWasSpace = true;
|
|
|
|
|
List<char> final = new List<char>();
|
|
|
|
|
List<int> spaces = new List<int>();
|
|
|
|
|
for (int i = 0; i < symbols.Count(); i++)
|
|
|
|
|
{
|
|
|
|
|
if (symbols[i] == symbolsTemplate[iTemp])
|
|
|
|
|
{
|
|
|
|
|
isTemplate = true;
|
|
|
|
|
iTemp++;
|
|
|
|
|
if (iTemp >= symbolsTemplate.Count)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
while (i < symbols.Count())
|
|
|
|
|
{
|
|
|
|
|
final.Add(symbols[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
isTemplate = false;
|
|
|
|
|
}
|
|
|
|
|
if (!isTemplate)
|
|
|
|
|
{
|
|
|
|
|
final.Add(symbols[i]);
|
|
|
|
|
islastWasSpace = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!islastWasSpace)
|
|
|
|
|
{
|
|
|
|
|
final.Add(' ');
|
|
|
|
|
spaces.Add(final.Count - 1);
|
|
|
|
|
islastWasSpace = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
final.Add(' ');
|
|
|
|
|
spaces.Add(final.Count - 1);
|
|
|
|
|
|
|
|
|
|
List<string> words = new List<string>();
|
|
|
|
|
StringBuilder w = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < final.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
bool space = false;
|
|
|
|
|
for (int j = 0; j < spaces.Count; j++)
|
|
|
|
|
{
|
|
|
|
|
if (i == spaces[j])
|
|
|
|
|
{
|
|
|
|
|
space = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (space)
|
|
|
|
|
{
|
|
|
|
|
words.Add(w.ToString());
|
|
|
|
|
w.Remove(0, w.Length);
|
|
|
|
|
i++;
|
|
|
|
|
if ( i == final.Count)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
w.Append(final[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder t = new StringBuilder(template);
|
|
|
|
|
int wordind = 0;
|
|
|
|
|
for (int i = 0; i < t.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (t[i] == '{')
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
while (t[i] != '}')
|
|
|
|
|
{
|
|
|
|
|
t.Remove(i, 1);
|
|
|
|
|
}
|
|
|
|
|
t.Insert(i, words[wordind], 1);
|
|
|
|
|
wordind++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sb = t;
|
2024-09-21 21:03:55 +03:00
|
|
|
|
|
2024-09-20 03:20:29 +03:00
|
|
|
|
foreach (var property in typeof(T).GetProperties())
|
|
|
|
|
{
|
|
|
|
|
if (!property.CanWrite)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-21 21:03:55 +03:00
|
|
|
|
//MessageBox.Show(property.Name);
|
|
|
|
|
|
2024-09-20 03:20:29 +03:00
|
|
|
|
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 (listBox.Items.Count <= rowIndex)
|
|
|
|
|
{
|
|
|
|
|
listBox.Items.Add(template);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string row = listBox.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());
|
|
|
|
|
listBox.Items[rowIndex] = row;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int CountRows()
|
|
|
|
|
{
|
|
|
|
|
return listBox.Items.Count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|