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; 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() where T : class, new() { if (listBox.SelectedIndex < 0) { return null; } string row = listBox.SelectedItem.ToString(); T curObject = new T(); StringBuilder sb = new StringBuilder(row); MessageBox.Show(sb.ToString()); //("Дорогой дневник, мне не подобрать слов чтобы описать всю {Mood}, что я испытал сегодня; {Date}", "{", "}"); // "Дорогой дневник, мне не подобрать слов чтобы описать всю радость пенис, что я испытал сегодня; блаблабла" StringBuilder tp = new StringBuilder(template); string cleanTemplate = Regex.Replace(template, @"\{.*?\}", ""); List symbolsTemplate = cleanTemplate.ToCharArray().ToList(); List symbols = row.ToCharArray().ToList(); int iTemp = 0; bool isTemplate = true; bool islastWasSpace = true; List final = new List(); List spaces = new List(); 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 words = new List(); 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; 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 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; } } }