Сдано
This commit is contained in:
parent
b5f9121ce7
commit
0b05d77cdd
@ -15,6 +15,8 @@ namespace Library_var_4_lab_1
|
||||
public partial class CustomListBox : UserControl
|
||||
{
|
||||
private List<string> properties = new();
|
||||
string StartSimbol = string.Empty;
|
||||
string EndSimbol = string.Empty;
|
||||
private List<List<object>> values = new List<List<object>>();
|
||||
private string Template = string.Empty;
|
||||
private Regex Regex = new(string.Empty);
|
||||
@ -24,10 +26,12 @@ namespace Library_var_4_lab_1
|
||||
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;
|
||||
StartSimbol = startSimbol;
|
||||
EndSimbol = endSimbol;
|
||||
Regex = new Regex(pattern);
|
||||
properties.Clear();
|
||||
listBox.Items.Clear();
|
||||
@ -54,34 +58,50 @@ namespace Library_var_4_lab_1
|
||||
|
||||
public void Add<T>(T Object)
|
||||
{
|
||||
List<string> tmpList = new();
|
||||
tmpList.AddRange(properties);
|
||||
string[] splitStr = Regex.Split(Template);
|
||||
string resultString = string.Empty;
|
||||
for(int i = 0; i < splitStr.Length-1; i += 2)
|
||||
var tmpTemplate = Template;
|
||||
int i = 0;
|
||||
foreach (var property in properties)
|
||||
{
|
||||
resultString = string.Concat(resultString, splitStr[i]);
|
||||
if (tmpList.Count > 0)
|
||||
{
|
||||
var tmpValue = typeof(T)?.GetProperty(tmpList.First())?.GetValue(Object);
|
||||
resultString = string.Concat(resultString, tmpValue?.ToString() ?? string.Empty);
|
||||
values[properties.Count - tmpList.Count].Add(tmpValue ?? string.Empty);
|
||||
tmpList.RemoveAt(0);
|
||||
}
|
||||
var tmpValue = typeof(T)?.GetProperty(property)?.GetValue(Object);
|
||||
tmpTemplate = tmpTemplate.Replace(StartSimbol + property + EndSimbol, tmpValue?.ToString() ?? string.Empty);
|
||||
values[i].Add(tmpValue ?? string.Empty);
|
||||
i++;
|
||||
|
||||
}
|
||||
listBox.Items.Add(resultString);
|
||||
listBox.Items.Add(tmpTemplate);
|
||||
}
|
||||
|
||||
public T GetSelected<T>() where T : class, new()
|
||||
{
|
||||
Type objectType = typeof(T);
|
||||
T Object = new T();
|
||||
for (int i = 0; i < properties.Count; i++)
|
||||
string SelectedStr = "";
|
||||
if (listBox.SelectedIndex != -1)
|
||||
{
|
||||
var field = objectType.GetProperty(properties[i]);
|
||||
field?.SetValue(Object, values[i][listBox.SelectedIndex]);
|
||||
SelectedStr = listBox.SelectedItem.ToString();
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -27,19 +27,8 @@ namespace Library_var_4_lab_1
|
||||
|
||||
public string SelectedItem
|
||||
{
|
||||
get => comboBox.SelectedIndex != -1 ? comboBox.SelectedItem.ToString() ?? string.Empty : string.Empty;
|
||||
set
|
||||
{
|
||||
if (comboBox.Items.Contains(value))
|
||||
{
|
||||
comboBox.SelectedItem = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
comboBox.SelectedIndex = -1;
|
||||
}
|
||||
|
||||
}
|
||||
get => comboBox.SelectedItem?.ToString() ?? string.Empty;
|
||||
set => comboBox.SelectedIndex = comboBox.Items.IndexOf(value);
|
||||
}
|
||||
|
||||
public void Input(string value)
|
||||
|
@ -10,6 +10,6 @@ namespace TestProj
|
||||
{
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public string SurName { get; set; } = string.Empty;
|
||||
public int? Age { get; set; }
|
||||
public int Age { get; set; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user