diff --git a/Cop_25/Controls/CustomListBox.cs b/Cop_25/Controls/CustomListBox.cs index 3da3c13..c52c023 100644 --- a/Cop_25/Controls/CustomListBox.cs +++ b/Cop_25/Controls/CustomListBox.cs @@ -22,65 +22,80 @@ namespace Controls InitializeComponent(); } - /// - /// Макет - /// - private string _templateString; + private string template; + private string start; + private string end; - /// - /// Символ начала - /// - private string _startSymbol; - - /// - /// Символ конца - /// - private string _endSymbol; - - /// - /// Шаблон строки - /// - public void SetTemplateString(string templateString, string startSymbol, string endSymbol) + public void setTemplate(string _template, string _start, string _end) { - if (templateString != "" && startSymbol != "" && endSymbol != "") + if (string.IsNullOrEmpty(_template) || string.IsNullOrEmpty(_start) || string.IsNullOrEmpty(_end)) { - _templateString = templateString; - _startSymbol = startSymbol; - _endSymbol = endSymbol; + throw new ArgumentNullException("Wrong template"); } - else + template = _template; + start = _start; + end = _end; + } + + public int SelectedRow + { + get { - throw new ArgumentNullException("Вы не ввели все значения"); + return listBoxMain.SelectedIndex; + } + set + { + if (value < 0) return; + listBoxMain.SelectedIndex = value; } } - /// - /// Выбранная строка - /// - public int SelectedIndex - { - get { return listBoxMain.SelectedIndex; } - set - { - if (listBoxMain.SelectedIndex != 0) - { - listBoxMain.SelectedIndex = value; - } - } - } - - /// - /// Получение обхекта - /// public T GetObjectFromStr() where T : class, new() { - if (listBoxMain.SelectedIndex == -1) + + 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 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) @@ -88,34 +103,32 @@ namespace Controls continue; } - int borderOne = sb.ToString().IndexOf(_startSymbol); - if (borderOne == -1) + //MessageBox.Show(property.Name); + + int startBorder = sb.ToString().IndexOf(start); + if (startBorder == -1) { break; } - int borderTwo = sb.ToString().IndexOf(_endSymbol, borderOne + 1); - if (borderTwo == -1) + int endBorder = sb.ToString().IndexOf(end, startBorder + 1); + if (endBorder == -1) { break; } - string propertyValue = sb.ToString(borderOne + 1, borderTwo - borderOne - 1); - sb.Remove(0, borderTwo + 1); + 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 (listBoxMain.Items.Count <= rowIndex) { - listBoxMain.Items.Add(_templateString); + listBoxMain.Items.Add(template); } string row = listBoxMain.Items[rowIndex].ToString(); @@ -123,10 +136,15 @@ namespace Controls if (propertyInfo != null) { - object propertyValue = propertyInfo.GetValue(dataObject); - row = row.Replace($"{_startSymbol}{propertyName}{_endSymbol}", propertyValue.ToString()); + var propertyValue = propertyInfo.GetValue(dataObject); + row = row.Replace($"{start}{propertyName}{end}", propertyValue.ToString()); listBoxMain.Items[rowIndex] = row; } } + + public int CountRows() + { + return listBoxMain.Items.Count; + } } } diff --git a/Cop_25/Forms/FormMain.Designer.cs b/Cop_25/Forms/FormMain.Designer.cs index 6c57cf4..e5b03ab 100644 --- a/Cop_25/Forms/FormMain.Designer.cs +++ b/Cop_25/Forms/FormMain.Designer.cs @@ -39,6 +39,7 @@ button2 = new Button(); pictureBox1 = new PictureBox(); textBox1 = new TextBox(); + customListBox1 = new Controls.CustomListBox(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); SuspendLayout(); // @@ -96,11 +97,20 @@ textBox1.TextAlign = HorizontalAlignment.Center; textBox1.TextChanged += textBox1_TextChanged; // + // customListBox1 + // + customListBox1.Location = new Point(378, 43); + customListBox1.Name = "customListBox1"; + customListBox1.SelectedRow = -1; + customListBox1.Size = new Size(608, 265); + customListBox1.TabIndex = 14; + // // FormMain // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(806, 381); + ClientSize = new Size(1123, 381); + Controls.Add(customListBox1); Controls.Add(textBox1); Controls.Add(button2); Controls.Add(buttonLargeText); @@ -128,5 +138,6 @@ private Button button2; private PictureBox pictureBox1; private TextBox textBox1; + private Controls.CustomListBox customListBox1; } } \ No newline at end of file diff --git a/Cop_25/Forms/FormMain.cs b/Cop_25/Forms/FormMain.cs index 8fb21b4..feec716 100644 --- a/Cop_25/Forms/FormMain.cs +++ b/Cop_25/Forms/FormMain.cs @@ -19,7 +19,25 @@ namespace Forms public FormMain() { InitializeComponent(); + List people = new List() + { + new Person("aboba", "not aboba", "19"), + new Person("aboba2", "not aboba2", "25"), + new Person("aboba3", "not aboba3", "20"), + }; + customListBox1.setTemplate("Дорогой дневниов чтобы описать всю {FirstName} {LastName} возраста {Age}", "{", "}"); + customListBox1.FillProperty(people[0], 0, "FirstName"); + customListBox1.FillProperty(people[0], 0, "LastName"); + customListBox1.FillProperty(people[0], 0, "Age"); + + customListBox1.FillProperty(people[1], 1, "FirstName"); + customListBox1.FillProperty(people[1], 1, "LastName"); + customListBox1.FillProperty(people[1], 1, "Age"); + + customListBox1.FillProperty(people[2], 2, "FirstName"); + customListBox1.FillProperty(people[2], 2, "LastName"); + customListBox1.FillProperty(people[2], 2, "Age"); } private void buttonLargeText_CLick(object sender, EventArgs e) @@ -72,5 +90,6 @@ namespace Forms { } + } }