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;
|
|
|
|
|
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-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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|