можете сдавать

This commit is contained in:
Леонид Малафеев 2024-10-22 22:57:47 +04:00
parent b7276eb339
commit 64c5d64d30
3 changed files with 105 additions and 57 deletions

View File

@ -22,65 +22,80 @@ namespace Controls
InitializeComponent(); InitializeComponent();
} }
/// <summary> private string template;
/// Макет private string start;
/// </summary> private string end;
private string _templateString;
/// <summary> public void setTemplate(string _template, string _start, string _end)
/// Символ начала
/// </summary>
private string _startSymbol;
/// <summary>
/// Символ конца
/// </summary>
private string _endSymbol;
/// <summary>
/// Шаблон строки
/// </summary>
public void SetTemplateString(string templateString, string startSymbol, string endSymbol)
{ {
if (templateString != "" && startSymbol != "" && endSymbol != "") if (string.IsNullOrEmpty(_template) || string.IsNullOrEmpty(_start) || string.IsNullOrEmpty(_end))
{ {
_templateString = templateString; throw new ArgumentNullException("Wrong template");
_startSymbol = startSymbol;
_endSymbol = endSymbol;
}
else
{
throw new ArgumentNullException("Вы не ввели все значения");
} }
template = _template;
start = _start;
end = _end;
} }
/// <summary> public int SelectedRow
/// Выбранная строка
/// </summary>
public int SelectedIndex
{ {
get { return listBoxMain.SelectedIndex; } get
{
return listBoxMain.SelectedIndex;
}
set set
{ {
if (listBoxMain.SelectedIndex != 0) if (value < 0) return;
{ listBoxMain.SelectedIndex = value;
listBoxMain.SelectedIndex = value;
}
} }
} }
/// <summary>
/// Получение обхекта
/// </summary>
public T GetObjectFromStr<T>() where T : class, new() public T GetObjectFromStr<T>() where T : class, new()
{ {
if (listBoxMain.SelectedIndex == -1)
if (listBoxMain.SelectedIndex < 0)
{ {
return null; return null;
} }
string row = listBoxMain.SelectedItem.ToString(); string row = listBoxMain.SelectedItem.ToString();
T curObject = new T(); T curObject = new T();
StringBuilder sb = new StringBuilder(row); 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<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;
foreach (var property in typeof(T).GetProperties()) foreach (var property in typeof(T).GetProperties())
{ {
if (!property.CanWrite) if (!property.CanWrite)
@ -88,34 +103,32 @@ namespace Controls
continue; continue;
} }
int borderOne = sb.ToString().IndexOf(_startSymbol); //MessageBox.Show(property.Name);
if (borderOne == -1)
int startBorder = sb.ToString().IndexOf(start);
if (startBorder == -1)
{ {
break; break;
} }
int borderTwo = sb.ToString().IndexOf(_endSymbol, borderOne + 1); int endBorder = sb.ToString().IndexOf(end, startBorder + 1);
if (borderTwo == -1) if (endBorder == -1)
{ {
break; break;
} }
string propertyValue = sb.ToString(borderOne + 1, borderTwo - borderOne - 1); string propertyValue = sb.ToString(startBorder + 1, endBorder - startBorder - 1);
sb.Remove(0, borderTwo + 1); sb.Remove(0, endBorder + 1);
property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType)); property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType));
} }
return curObject; return curObject;
} }
/// <summary>
/// Заполнение
/// </summary>
public void FillProperty<T>(T dataObject, int rowIndex, string propertyName) public void FillProperty<T>(T dataObject, int rowIndex, string propertyName)
{ {
while (listBoxMain.Items.Count <= rowIndex) while (listBoxMain.Items.Count <= rowIndex)
{ {
listBoxMain.Items.Add(_templateString); listBoxMain.Items.Add(template);
} }
string row = listBoxMain.Items[rowIndex].ToString(); string row = listBoxMain.Items[rowIndex].ToString();
@ -123,10 +136,15 @@ namespace Controls
if (propertyInfo != null) if (propertyInfo != null)
{ {
object propertyValue = propertyInfo.GetValue(dataObject); var propertyValue = propertyInfo.GetValue(dataObject);
row = row.Replace($"{_startSymbol}{propertyName}{_endSymbol}", propertyValue.ToString()); row = row.Replace($"{start}{propertyName}{end}", propertyValue.ToString());
listBoxMain.Items[rowIndex] = row; listBoxMain.Items[rowIndex] = row;
} }
} }
public int CountRows()
{
return listBoxMain.Items.Count;
}
} }
} }

View File

@ -39,6 +39,7 @@
button2 = new Button(); button2 = new Button();
pictureBox1 = new PictureBox(); pictureBox1 = new PictureBox();
textBox1 = new TextBox(); textBox1 = new TextBox();
customListBox1 = new Controls.CustomListBox();
((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit(); ((System.ComponentModel.ISupportInitialize)pictureBox1).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -96,11 +97,20 @@
textBox1.TextAlign = HorizontalAlignment.Center; textBox1.TextAlign = HorizontalAlignment.Center;
textBox1.TextChanged += textBox1_TextChanged; 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 // FormMain
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(806, 381); ClientSize = new Size(1123, 381);
Controls.Add(customListBox1);
Controls.Add(textBox1); Controls.Add(textBox1);
Controls.Add(button2); Controls.Add(button2);
Controls.Add(buttonLargeText); Controls.Add(buttonLargeText);
@ -128,5 +138,6 @@
private Button button2; private Button button2;
private PictureBox pictureBox1; private PictureBox pictureBox1;
private TextBox textBox1; private TextBox textBox1;
private Controls.CustomListBox customListBox1;
} }
} }

View File

@ -19,7 +19,25 @@ namespace Forms
public FormMain() public FormMain()
{ {
InitializeComponent(); InitializeComponent();
List<Person> people = new List<Person>()
{
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) private void buttonLargeText_CLick(object sender, EventArgs e)
@ -72,5 +90,6 @@ namespace Forms
{ {
} }
} }
} }