ready lab 1

This commit is contained in:
revengel66 2024-10-10 16:57:14 +03:00
parent 103dc552e0
commit 5489c642f0
3 changed files with 41 additions and 73 deletions

View File

@ -64,7 +64,7 @@ namespace ComponentsLibrary
foreach (var prop in properties)
{
string propertyValue = prop.GetValue(item)?.ToString() ?? string.Empty;
text = text.Replace("{" + prop.Name + "}", propertyValue);
text = text.Replace($"{startSymbol}{prop.Name}{endSymbol}", propertyValue);
}
return text;
@ -73,74 +73,46 @@ namespace ComponentsLibrary
// Публичный параметризованный метод для получения объекта из выбранной строки
public T? GetSelectedItem<T>() where T : new()
{
var item = listBox.SelectedItem;
if (item != null)
var selectedItem = listBox.SelectedItem;
if (selectedItem != null)
{
string selectedString = selectedItem.ToString(); // Строка из ListBox
T obj = new T();
PropertyInfo[] properties = typeof(T).GetProperties();
string layout = layoutString;
string pattern = @"^(.*?){T}.*?(.*?){P}";
//string input2 = "Температура воздуха ТЕМПЕРАТУРА ВЫСОКАЯ, давление НИЗКОЕ";
string? selectedString = item?.ToString();
string pattern = layoutString;
if (selectedString != null)
for (int i = 0; i < properties.Length; i++)
{
T obj = new T();
Regex regex = new Regex(pattern);
Match match = regex.Match(layout);
string[] staticText = new string[properties.Length];
if (match.Success)
{
// Извлекаем текст до {T} и до {P}
//string beforeT = match.Groups[1].Value;
//string beforeP = match.Groups[2].Value;
for (int i = 0; i < staticText.Length; i++)
{
staticText[i] = match.Groups[i+1].Value;
}
//string pattern2 = $@"{beforeT}(.*?)\{beforeP}(.*)\{beforeEnd}";
//"Температура воздуха (.*?)\\, давление (.*)\\, такие дела"
string patternStatic = $@"";
for (int i = 0; i < staticText.Length; i++)
{
if (i == staticText.Length - 1)
{
patternStatic += $"{staticText[i]}(.*)";
}
else {
patternStatic += $"{staticText[i]}(.*?)\\";
}
}
Regex regexValues = new Regex(patternStatic);
Match matchValues = regexValues.Match(selectedString);
if (matchValues.Success)
{
//object val1 = matchValues.Groups[1].Value;
//object val2 = matchValues.Groups[2].Value;
for (int i = 0; i < properties.Length; i++)
{
object value = Convert.ChangeType(matchValues.Groups[i+1].Value, properties[i].PropertyType);
properties[i].SetValue(obj, value);
}
return obj;
}
PropertyInfo prop = properties[i];
string propertyPattern = $"{startSymbol}{prop.Name}{endSymbol}";
if (i == properties.Length - 1)
{
pattern = pattern.Replace(propertyPattern, "(.*)");
}
else
{
pattern = pattern.Replace(propertyPattern, "(.*?)");
}
}
}
Regex regex = new Regex(pattern);
Match match = regex.Match(selectedString);
if (match.Success)
{
for (int i = 0; i < properties.Length; i++)
{
string value = match.Groups[i + 1].Value;
object convertedValue = Convert.ChangeType(value, properties[i].PropertyType);
properties[i].SetValue(obj, convertedValue);
}
return obj;
}
}
return default;
}
}
}

View File

@ -60,26 +60,23 @@ namespace ComponentsLab
private void listBoxValues_Load(object sender, EventArgs e)
{
listBoxValues.SetLayout("Òåìïåðàòóðà âîçäóõà {T}, äàâëåíèå {P}", '{', '}');
listBoxValues.SetLayout("Òåìïåðàòóðà âîçäóõà [Temp], äàâëåíèå [Pressure]", '[', ']');
var objectList = new List<ObjectClass>
{
new ObjectClass { T = "òåìïåðàòóðà âûñîêàÿ", P = "1008" },
new ObjectClass { T = "òåìïåðàòóðà2", P = "1008" },
new ObjectClass { T = "òåìïåðàòóðà3", P = 1010 },
new ObjectClass { T = "òåìïåðàòóðà4", P = 1011 },
new ObjectClass { T = "òåìïåðàòóðà5", P = 1009 },
new ObjectClass { Temp = "òåìïåðàòóðà âûñîêàÿ", Pressure = "1008" },
new ObjectClass { Temp = "òåìïåðàòóðà2", Pressure = "1008" },
new ObjectClass { Temp = "òåìïåðàòóðà3", Pressure = 1010 },
new ObjectClass { Temp = "òåìïåðàòóðà4", Pressure = 1011 },
new ObjectClass { Temp = "òåìïåðàòóðà5", Pressure = 1009 },
};
listBoxValues.FillListBox(objectList);
}
private void listBoxValues_GetObject(object sender, EventArgs e)
{
try
{
var selectedItem = listBoxValues.GetSelectedItem<ObjectClass>();
checkParametrs();
checkParametrs();
}
catch (EmailException ex)
{
@ -90,10 +87,9 @@ namespace ComponentsLab
private void checkParametrs()
{
ObjectClass obj = listBoxValues.GetSelectedItem<ObjectClass>();
object? T = obj?.T;
object? P = obj?.P;
object? T = obj?.Temp;
object? P = obj?.Pressure;
MessageBox.Show($"Îáúåêò ñîçäàí, ïåðâîå çíà÷åíèå: {T}, âòîðîå çíà÷åíèå: {P}");
}
}
}

View File

@ -2,7 +2,7 @@
{
public class ObjectClass
{
public object T { get; set; }
public object P { get; set; }
public object? Temp { get; set; }
public object? Pressure { get; set; }
}
}