Compare commits

..

2 Commits

8 changed files with 235 additions and 205 deletions

View File

@ -69,7 +69,7 @@ namespace ComponentsLibrary
{
if (string.IsNullOrEmpty(emailPattern))
{
return true; // Если шаблон не задан, любая строка считается валидной.
return true;
}
return Regex.IsMatch(email, emailPattern);
}

View File

@ -1,5 +1,6 @@
using System.Reflection;
using System.Text.RegularExpressions;
using static System.Net.Mime.MediaTypeNames;
namespace ComponentsLibrary
{
@ -62,38 +63,84 @@ namespace ComponentsLibrary
foreach (var prop in properties)
{
string propertyValue = prop.GetValue(item)?.ToString().Replace(',', '.') ?? string.Empty;
string propertyValue = prop.GetValue(item)?.ToString() ?? string.Empty;
text = text.Replace("{" + prop.Name + "}", propertyValue);
}
return text;
}
// Публичный параметризованный метод для получения объекта из выбранной строки
public T? GetSelectedItem<T>() where T : new()
{
var item = listBox.SelectedItem;
if (item != null)
{
// Получаем строковое значение
string? selectedString = item?.ToString();
if (selectedString != null) {
T obj = new T();
// Публичный параметризованный метод для получения объекта из выбранной строки
public T? GetSelectedItem<T>() where T : new()
{
var item = listBox.SelectedItem;
if (item != null)
{
MatchCollection matches = Regex.Matches(selectedString, @"\d+(\,\d+)?");
PropertyInfo[] properties = typeof(T).GetProperties();
string layout = layoutString;
string pattern = @"^(.*?){T}.*?(.*?){P}";
PropertyInfo[] properties = typeof(T).GetProperties();
//string input2 = "Температура воздуха ТЕМПЕРАТУРА ВЫСОКАЯ, давление НИЗКОЕ";
string? selectedString = item?.ToString();
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;
}
}
if (selectedString != null)
{
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;
}
}
}
}
return default;
}
}
}

View File

@ -20,55 +20,43 @@
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
comboBoxComponent = new ComboBox();
buttonClear = new Button();
SuspendLayout();
//
// comboBoxComponent
//
comboBoxComponent.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxComponent.FormattingEnabled = true;
comboBoxComponent.Items.AddRange(new object[] { "Значение 1", "Значение 2", "Значение 3", "Значение 4", "Значение 5" });
comboBoxComponent.Location = new Point(15, 23);
comboBoxComponent.Name = "comboBoxComponent";
comboBoxComponent.Size = new Size(121, 23);
comboBoxComponent.TabIndex = 0;
comboBoxComponent.SelectedIndexChanged += comboBoxComponent_SelectedIndexChanged;
//
// buttonClear
//
buttonClear.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
buttonClear.Location = new Point(16, 56);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(120, 23);
buttonClear.TabIndex = 1;
buttonClear.Text = "Очистить";
buttonClear.UseVisualStyleBackColor = true;
buttonClear.Click += buttonClear_Click;
//
// VisualSelectionComponent
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
BorderStyle = BorderStyle.FixedSingle;
Controls.Add(buttonClear);
Controls.Add(comboBoxComponent);
Name = "VisualSelectionComponent";
Size = new Size(148, 94);
ResumeLayout(false);
}
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
comboBoxComponent = new ComboBox();
SuspendLayout();
//
// comboBoxComponent
//
comboBoxComponent.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxComponent.FormattingEnabled = true;
comboBoxComponent.Items.AddRange(new object[] { "Значение 1", "Значение 2", "Значение 3", "Значение 4", "Значение 5" });
comboBoxComponent.Location = new Point(17, 31);
comboBoxComponent.Margin = new Padding(3, 4, 3, 4);
comboBoxComponent.Name = "comboBoxComponent";
comboBoxComponent.Size = new Size(138, 28);
comboBoxComponent.TabIndex = 0;
comboBoxComponent.SelectedIndexChanged += comboBoxComponent_SelectedIndexChanged;
//
// VisualSelectionComponent
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
BorderStyle = BorderStyle.FixedSingle;
Controls.Add(comboBoxComponent);
Margin = new Padding(3, 4, 3, 4);
Name = "VisualSelectionComponent";
Size = new Size(169, 87);
ResumeLayout(false);
}
#endregion
#endregion
private ComboBox comboBoxComponent;
private Button buttonClear;
private ComboBox comboBoxComponent;
}
}

View File

@ -11,24 +11,14 @@ namespace ComponentsLab
//Публичное свойство (set, get) для установки и получения выбранного значения (возвращает пустую строку, если нет выбранного значения)
public string comboBoxSelectedValue
{
get
{
if (comboBoxComponent.SelectedItem != null)
{
return comboBoxComponent.GetItemText(comboBoxComponent.SelectedItem);
}
else
{
return string.Empty;
}
}
set
{
if (comboBoxComponent.SelectedIndex != -1)
{
comboBoxComponent.Items[comboBoxComponent.SelectedIndex] = value;
}
}
get
{
return comboBoxComponent.SelectedItem?.ToString() ?? string.Empty;
}
set
{
comboBoxComponent.SelectedItem = value;
}
}
// Событие, вызываемое при смене значения в ComboBox
@ -42,13 +32,10 @@ namespace ComponentsLab
_changeComboBox?.Invoke(this, e);
}
// Отдельный публичный метод отчистки списка
private void buttonClear_Click(object sender, EventArgs e)
// Отдельный публичный метод отчистки списка
public void clearList()
{
comboBoxComponent.Items.Clear();
comboBoxComponent.Text = string.Empty;
comboBoxComponent.SelectedItem = null;
MessageBox.Show($"The list has been cleared");
}

View File

@ -20,107 +20,101 @@
base.Dispose(disposing);
}
#region Windows Form Designer generated code
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
imageLoad = new ImageLoad();
visualSelectionComponent1 = new VisualSelectionComponent();
emailComponent = new ComponentsLibrary.Email();
buttonCheckEmail = new Button();
listBoxValues = new ComponentsLibrary.ListBoxValues();
buttonGetListSelectedItem = new Button();
SuspendLayout();
//
// imageLoad
//
imageLoad.Avatar = null;
imageLoad.BorderStyle = BorderStyle.FixedSingle;
imageLoad.Location = new Point(12, 12);
imageLoad.Name = "imageLoad";
imageLoad.Size = new Size(147, 92);
imageLoad.TabIndex = 0;
imageLoad.AvatarChanged += ImageLoad_AvatarChanged;
//
// visualSelectionComponent1
//
visualSelectionComponent1.BorderStyle = BorderStyle.FixedSingle;
visualSelectionComponent1.comboBoxSelectedValue = "";
visualSelectionComponent1.Location = new Point(181, 12);
visualSelectionComponent1.Name = "visualSelectionComponent1";
visualSelectionComponent1.Size = new Size(156, 92);
visualSelectionComponent1.TabIndex = 1;
visualSelectionComponent1.ChangeComboBox += visualSelectionComponent1_ChangeComboBox;
//
// emailComponent
//
emailComponent.BorderStyle = BorderStyle.FixedSingle;
emailComponent.EmailPattern = null;
emailComponent.Location = new Point(356, 12);
emailComponent.Name = "emailComponent";
emailComponent.Size = new Size(172, 92);
emailComponent.TabIndex = 2;
emailComponent.ChangeEmail += emailComponent_ChangeEmail;
//
// buttonCheckEmail
//
buttonCheckEmail.Location = new Point(374, 66);
buttonCheckEmail.Name = "buttonCheckEmail";
buttonCheckEmail.Size = new Size(136, 23);
buttonCheckEmail.TabIndex = 3;
buttonCheckEmail.Text = "Проверить email";
buttonCheckEmail.UseVisualStyleBackColor = true;
buttonCheckEmail.Click += buttonCheckEmail_Click;
//
// listBoxValues
//
listBoxValues.BorderStyle = BorderStyle.FixedSingle;
listBoxValues.Location = new Point(12, 110);
listBoxValues.Name = "listBoxValues";
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(551, 376);
Controls.Add(buttonGetListSelectedItem);
Controls.Add(listBoxValues);
Controls.Add(buttonCheckEmail);
Controls.Add(emailComponent);
Controls.Add(visualSelectionComponent1);
Controls.Add(imageLoad);
Name = "FormComponents";
Text = "FormComponents";
ResumeLayout(false);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
imageLoad = new ImageLoad();
visualSelectionComponent1 = new VisualSelectionComponent();
emailComponent = new ComponentsLibrary.Email();
buttonCheckEmail = new Button();
listBoxValues = new ComponentsLibrary.ListBoxValues();
SuspendLayout();
//
// imageLoad
//
imageLoad.Avatar = null;
imageLoad.BorderStyle = BorderStyle.FixedSingle;
imageLoad.Location = new Point(14, 16);
imageLoad.Margin = new Padding(3, 5, 3, 5);
imageLoad.Name = "imageLoad";
imageLoad.Size = new Size(168, 122);
imageLoad.TabIndex = 0;
imageLoad.AvatarChanged += ImageLoad_AvatarChanged;
//
// visualSelectionComponent1
//
visualSelectionComponent1.BorderStyle = BorderStyle.FixedSingle;
visualSelectionComponent1.comboBoxSelectedValue = "";
visualSelectionComponent1.Location = new Point(207, 16);
visualSelectionComponent1.Margin = new Padding(3, 5, 3, 5);
visualSelectionComponent1.Name = "visualSelectionComponent1";
visualSelectionComponent1.Size = new Size(178, 122);
visualSelectionComponent1.TabIndex = 1;
visualSelectionComponent1.ChangeComboBox += visualSelectionComponent1_ChangeComboBox;
//
// emailComponent
//
emailComponent.BorderStyle = BorderStyle.FixedSingle;
emailComponent.EmailPattern = null;
emailComponent.Location = new Point(407, 16);
emailComponent.Margin = new Padding(3, 5, 3, 5);
emailComponent.Name = "emailComponent";
emailComponent.Size = new Size(196, 122);
emailComponent.TabIndex = 2;
emailComponent.ChangeEmail += emailComponent_ChangeEmail;
//
// buttonCheckEmail
//
buttonCheckEmail.Location = new Point(427, 88);
buttonCheckEmail.Margin = new Padding(3, 4, 3, 4);
buttonCheckEmail.Name = "buttonCheckEmail";
buttonCheckEmail.Size = new Size(155, 31);
buttonCheckEmail.TabIndex = 3;
buttonCheckEmail.Text = "Проверить email";
buttonCheckEmail.UseVisualStyleBackColor = true;
buttonCheckEmail.Click += buttonCheckEmail_Click;
//
// listBoxValues
//
listBoxValues.BorderStyle = BorderStyle.FixedSingle;
listBoxValues.Location = new Point(14, 147);
listBoxValues.Margin = new Padding(3, 5, 3, 5);
listBoxValues.Name = "listBoxValues";
listBoxValues.SelectedIndex = -1;
listBoxValues.Size = new Size(589, 298);
listBoxValues.TabIndex = 4;
listBoxValues.GetObject += listBoxValues_GetObject;
listBoxValues.Load += listBoxValues_Load;
//
// FormComponents
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(630, 501);
Controls.Add(listBoxValues);
Controls.Add(buttonCheckEmail);
Controls.Add(emailComponent);
Controls.Add(visualSelectionComponent1);
Controls.Add(imageLoad);
Margin = new Padding(3, 4, 3, 4);
Name = "FormComponents";
Text = "FormComponents";
ResumeLayout(false);
}
#endregion
#endregion
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private ComponentsLab.ImageLoad imageLoad;
private ComponentsLab.VisualSelectionComponent visualSelectionComponent1;
private ComponentsLibrary.Email emailComponent;
private Button buttonCheckEmail;
private ComponentsLibrary.ListBoxValues listBoxValues;
private Button buttonGetListSelectedItem;
}
}

View File

@ -63,11 +63,11 @@ namespace ComponentsLab
listBoxValues.SetLayout("Òåìïåðàòóðà âîçäóõà {T}, äàâëåíèå {P}", '{', '}');
var objectList = new List<ObjectClass>
{
new ObjectClass { T = 20.5, P = 1012 },
new ObjectClass { T = 18.3, P = 1008 },
new ObjectClass { T = 50.1, P = 1010 },
new ObjectClass { T = 30.0, P = 1011 },
new ObjectClass { T = 18.9, P = 1009 },
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 },
};
listBoxValues.FillListBox(objectList);
}
@ -77,12 +77,23 @@ namespace ComponentsLab
try
{
var selectedItem = listBoxValues.GetSelectedItem<ObjectClass>();
MessageBox.Show($"Îáúĺęň ńîçäŕí: {selectedItem}");
checkParametrs();
}
catch (EmailException ex)
{
MessageBox.Show(ex.Message, $"Îøèáêà ïðè çàïîëíåíèè ñâîéñòâ îáúåêòà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void checkParametrs()
{
ObjectClass obj = listBoxValues.GetSelectedItem<ObjectClass>();
object? T = obj?.T;
object? P = obj?.P;
MessageBox.Show($"Îáúåêò ñîçäàí, ïåðâîå çíà÷åíèå: {T}, âòîðîå çíà÷åíèå: {P}");
}
}
}

View File

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

View File

@ -1,3 +1,5 @@
using System.Text.RegularExpressions;
namespace ComponentsLab
{
internal static class Program
@ -8,10 +10,11 @@ namespace ComponentsLab
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormComponents());
}
}
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormComponents());
}
}
}