ready lab1
This commit is contained in:
parent
ce79918c9b
commit
f098199010
1
ComponentsLibrary/ListBoxValues.Designer.cs
generated
1
ComponentsLibrary/ListBoxValues.Designer.cs
generated
@ -40,6 +40,7 @@
|
||||
listBox.Name = "listBox";
|
||||
listBox.Size = new Size(222, 124);
|
||||
listBox.TabIndex = 0;
|
||||
listBox.SelectedIndexChanged += listBox_SelectedIndexChanged;
|
||||
//
|
||||
// ListBoxValues
|
||||
//
|
||||
|
@ -5,16 +5,27 @@ namespace ComponentsLibrary
|
||||
{
|
||||
public partial class ListBoxValues : UserControl
|
||||
{
|
||||
//private List<Employee> _employees;
|
||||
private string layoutString = string.Empty;
|
||||
private char startSymbol = '{';
|
||||
private char endSymbol = '}';
|
||||
private List<object> items = new List<object>();
|
||||
private EventHandler? _getObject;
|
||||
public ListBoxValues()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
//событие, вызываемое при выборе строки
|
||||
public event EventHandler GetObject
|
||||
{
|
||||
add { _getObject += value; }
|
||||
remove { _getObject -= value; }
|
||||
}
|
||||
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
_getObject?.Invoke(this, e);
|
||||
}
|
||||
|
||||
//Публичное свойство для установки и получения индекса выбранной строки (set, get).
|
||||
public int SelectedIndex
|
||||
{
|
||||
@ -36,35 +47,51 @@ namespace ComponentsLibrary
|
||||
items.Clear();
|
||||
foreach (var item in itemList)
|
||||
{
|
||||
if (item != null) {
|
||||
items.Add(item);
|
||||
string displayText = CreateDisplayText(item);
|
||||
listBox.Items.Add(displayText);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Метод для создания строки на основе макета
|
||||
private string CreateDisplayText(object item)
|
||||
{
|
||||
string text = layoutString;
|
||||
// Регулярное выражение для поиска имен свойств
|
||||
var regex = new Regex($@"{startSymbol}(\w+){endSymbol}");
|
||||
return regex.Replace(text, match =>
|
||||
PropertyInfo[] properties = item.GetType().GetProperties();
|
||||
|
||||
foreach (var prop in properties)
|
||||
{
|
||||
string propertyName = match.Groups[1].Value;
|
||||
PropertyInfo prop = item.GetType().GetProperty(propertyName);
|
||||
if (prop != null)
|
||||
{
|
||||
return prop.GetValue(item)?.ToString() ?? string.Empty;
|
||||
}
|
||||
return match.Value; // Возвращаем то, что нашли, если свойства нет
|
||||
});
|
||||
string propertyValue = prop.GetValue(item)?.ToString().Replace(',', '.') ?? string.Empty;
|
||||
text = text.Replace("{" + prop.Name + "}", propertyValue);
|
||||
}
|
||||
|
||||
return text;
|
||||
|
||||
}
|
||||
// Публичный параметризованный метод для получения объекта из выбранной строки
|
||||
public T GetSelectedItem<T>()
|
||||
public T? GetSelectedItem<T>() where T : new()
|
||||
{
|
||||
if (listBox.SelectedIndex != -1)
|
||||
var item = listBox.SelectedItem;
|
||||
if (item != null)
|
||||
{
|
||||
return (T)items[listBox.SelectedIndex];
|
||||
// Получаем строковое значение
|
||||
string? selectedString = item?.ToString();
|
||||
|
||||
if (selectedString != null) {
|
||||
T obj = new T();
|
||||
|
||||
MatchCollection matches = Regex.Matches(selectedString, @"\d+(\,\d+)?");
|
||||
|
||||
PropertyInfo[] properties = typeof(T).GetProperties();
|
||||
|
||||
for (int i = 0; i < properties.Length && i < matches.Count; i++)
|
||||
{
|
||||
object value = Convert.ChangeType(matches[i].Value.Replace(",", "."), properties[i].PropertyType);
|
||||
properties[i].SetValue(obj, value);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
return default;
|
||||
}
|
||||
|
15
ComponentsView/FormComponents.Designer.cs
generated
15
ComponentsView/FormComponents.Designer.cs
generated
@ -34,6 +34,7 @@
|
||||
emailComponent = new ComponentsLibrary.Email();
|
||||
buttonCheckEmail = new Button();
|
||||
listBoxValues = new ComponentsLibrary.ListBoxValues();
|
||||
buttonGetListSelectedItem = new Button();
|
||||
SuspendLayout();
|
||||
//
|
||||
// imageLoad
|
||||
@ -84,13 +85,24 @@
|
||||
listBoxValues.SelectedIndex = -1;
|
||||
listBoxValues.Size = new Size(516, 224);
|
||||
listBoxValues.TabIndex = 4;
|
||||
listBoxValues.GetObject += listBoxValues_GetObject;
|
||||
listBoxValues.Load += listBoxValues_Load;
|
||||
//
|
||||
// buttonGetListSelectedItem
|
||||
//
|
||||
buttonGetListSelectedItem.Location = new Point(12, 341);
|
||||
buttonGetListSelectedItem.Name = "buttonGetListSelectedItem";
|
||||
buttonGetListSelectedItem.Size = new Size(133, 23);
|
||||
buttonGetListSelectedItem.TabIndex = 5;
|
||||
buttonGetListSelectedItem.Text = "Получить объект";
|
||||
buttonGetListSelectedItem.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// FormComponents
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(7F, 15F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(549, 345);
|
||||
ClientSize = new Size(551, 376);
|
||||
Controls.Add(buttonGetListSelectedItem);
|
||||
Controls.Add(listBoxValues);
|
||||
Controls.Add(buttonCheckEmail);
|
||||
Controls.Add(emailComponent);
|
||||
@ -109,5 +121,6 @@
|
||||
private ComponentsLibrary.Email emailComponent;
|
||||
private Button buttonCheckEmail;
|
||||
private ComponentsLibrary.ListBoxValues listBoxValues;
|
||||
private Button buttonGetListSelectedItem;
|
||||
}
|
||||
}
|
||||
|
@ -71,5 +71,18 @@ namespace ComponentsLab
|
||||
};
|
||||
listBoxValues.FillListBox(objectList);
|
||||
}
|
||||
private void listBoxValues_GetObject(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
var selectedItem = listBoxValues.GetSelectedItem<ObjectClass>();
|
||||
MessageBox.Show($"Îáúåêò ñîçäàí: {selectedItem}");
|
||||
}
|
||||
catch (EmailException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message, $"Îøèáêà ïðè çàïîëíåíèè ñâîéñòâ îáúåêòà", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user