Сдано

This commit is contained in:
Artyom_Yashin 2024-09-25 10:35:18 +04:00
parent b5f9121ce7
commit 0b05d77cdd
3 changed files with 45 additions and 36 deletions

View File

@ -15,6 +15,8 @@ namespace Library_var_4_lab_1
public partial class CustomListBox : UserControl public partial class CustomListBox : UserControl
{ {
private List<string> properties = new(); private List<string> properties = new();
string StartSimbol = string.Empty;
string EndSimbol = string.Empty;
private List<List<object>> values = new List<List<object>>(); private List<List<object>> values = new List<List<object>>();
private string Template = string.Empty; private string Template = string.Empty;
private Regex Regex = new(string.Empty); private Regex Regex = new(string.Empty);
@ -24,10 +26,12 @@ namespace Library_var_4_lab_1
InitializeComponent(); InitializeComponent();
} }
public void SetTemplate(string StartSimbol, string EndSimbol, string template) public void SetTemplate(string startSimbol, string endSimbol, string template)
{ {
string pattern = "\\" + StartSimbol + "([^"+EndSimbol+"]*)\\" + EndSimbol; string pattern = "\\" + startSimbol + "([^"+endSimbol+"]*)\\" + endSimbol;
Template = template; Template = template;
StartSimbol = startSimbol;
EndSimbol = endSimbol;
Regex = new Regex(pattern); Regex = new Regex(pattern);
properties.Clear(); properties.Clear();
listBox.Items.Clear(); listBox.Items.Clear();
@ -54,34 +58,50 @@ namespace Library_var_4_lab_1
public void Add<T>(T Object) public void Add<T>(T Object)
{ {
List<string> tmpList = new(); var tmpTemplate = Template;
tmpList.AddRange(properties); int i = 0;
string[] splitStr = Regex.Split(Template); foreach (var property in properties)
string resultString = string.Empty;
for(int i = 0; i < splitStr.Length-1; i += 2)
{ {
resultString = string.Concat(resultString, splitStr[i]); var tmpValue = typeof(T)?.GetProperty(property)?.GetValue(Object);
if (tmpList.Count > 0) tmpTemplate = tmpTemplate.Replace(StartSimbol + property + EndSimbol, tmpValue?.ToString() ?? string.Empty);
{ values[i].Add(tmpValue ?? string.Empty);
var tmpValue = typeof(T)?.GetProperty(tmpList.First())?.GetValue(Object); i++;
resultString = string.Concat(resultString, tmpValue?.ToString() ?? string.Empty);
values[properties.Count - tmpList.Count].Add(tmpValue ?? string.Empty);
tmpList.RemoveAt(0);
}
} }
listBox.Items.Add(resultString); listBox.Items.Add(tmpTemplate);
} }
public T GetSelected<T>() where T : class, new() public T GetSelected<T>() where T : class, new()
{ {
Type objectType = typeof(T); string SelectedStr = "";
T Object = new T(); if (listBox.SelectedIndex != -1)
for (int i = 0; i < properties.Count; i++)
{ {
var field = objectType.GetProperty(properties[i]); SelectedStr = listBox.SelectedItem.ToString();
field?.SetValue(Object, values[i][listBox.SelectedIndex]);
} }
return Object;
T currentObject = new T();
Type objectType = typeof(T);
int rowIndex = listBox.SelectedIndex;
int colIndex = 0;
foreach (var prop in properties)
{
var field = objectType.GetProperty(prop);
if (!field?.CanWrite ?? false)
{
continue;
}
int startS = SelectedStr.IndexOf(values[colIndex][rowIndex].ToString());
if (startS == -1)
{
break;
}
string propValue = SelectedStr.Substring(startS, values[colIndex][rowIndex].ToString().Length);
if (SelectedStr.Length > startS + values[colIndex][rowIndex].ToString().Length + 1)
SelectedStr = SelectedStr.Substring(startS + values[colIndex][rowIndex].ToString().Length + 1);
field?.SetValue(currentObject, Convert.ChangeType(propValue, field.PropertyType));
colIndex++;
}
return currentObject;
} }
} }
} }

View File

@ -27,19 +27,8 @@ namespace Library_var_4_lab_1
public string SelectedItem public string SelectedItem
{ {
get => comboBox.SelectedIndex != -1 ? comboBox.SelectedItem.ToString() ?? string.Empty : string.Empty; get => comboBox.SelectedItem?.ToString() ?? string.Empty;
set set => comboBox.SelectedIndex = comboBox.Items.IndexOf(value);
{
if (comboBox.Items.Contains(value))
{
comboBox.SelectedItem = value;
}
else
{
comboBox.SelectedIndex = -1;
}
}
} }
public void Input(string value) public void Input(string value)

View File

@ -10,6 +10,6 @@ namespace TestProj
{ {
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public string SurName { get; set; } = string.Empty; public string SurName { get; set; } = string.Empty;
public int? Age { get; set; } public int Age { get; set; }
} }
} }