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)) if (string.IsNullOrEmpty(emailPattern))
{ {
return true; // Если шаблон не задан, любая строка считается валидной. return true;
} }
return Regex.IsMatch(email, emailPattern); return Regex.IsMatch(email, emailPattern);
} }

View File

@ -1,5 +1,6 @@
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using static System.Net.Mime.MediaTypeNames;
namespace ComponentsLibrary namespace ComponentsLibrary
{ {
@ -62,7 +63,7 @@ namespace ComponentsLibrary
foreach (var prop in properties) 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); text = text.Replace("{" + prop.Name + "}", propertyValue);
} }
@ -75,25 +76,71 @@ namespace ComponentsLibrary
var item = listBox.SelectedItem; var item = listBox.SelectedItem;
if (item != null) if (item != null)
{ {
// Получаем строковое значение
string? selectedString = item?.ToString();
if (selectedString != null) {
T obj = new T();
MatchCollection matches = Regex.Matches(selectedString, @"\d+(\,\d+)?");
PropertyInfo[] properties = typeof(T).GetProperties(); PropertyInfo[] properties = typeof(T).GetProperties();
string layout = layoutString;
string pattern = @"^(.*?){T}.*?(.*?){P}";
for (int i = 0; i < properties.Length && i < matches.Count; i++) //string input2 = "Температура воздуха ТЕМПЕРАТУРА ВЫСОКАЯ, давление НИЗКОЕ";
string? selectedString = item?.ToString();
if (selectedString != null)
{ {
object value = Convert.ChangeType(matches[i].Value.Replace(",", "."), properties[i].PropertyType); 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); properties[i].SetValue(obj, value);
} }
return obj; return obj;
} }
} }
}
}
return default; return default;
} }
} }
} }

View File

@ -29,7 +29,6 @@
private void InitializeComponent() private void InitializeComponent()
{ {
comboBoxComponent = new ComboBox(); comboBoxComponent = new ComboBox();
buttonClear = new Button();
SuspendLayout(); SuspendLayout();
// //
// comboBoxComponent // comboBoxComponent
@ -37,38 +36,27 @@
comboBoxComponent.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; comboBoxComponent.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxComponent.FormattingEnabled = true; comboBoxComponent.FormattingEnabled = true;
comboBoxComponent.Items.AddRange(new object[] { "Значение 1", "Значение 2", "Значение 3", "Значение 4", "Значение 5" }); comboBoxComponent.Items.AddRange(new object[] { "Значение 1", "Значение 2", "Значение 3", "Значение 4", "Значение 5" });
comboBoxComponent.Location = new Point(15, 23); comboBoxComponent.Location = new Point(17, 31);
comboBoxComponent.Margin = new Padding(3, 4, 3, 4);
comboBoxComponent.Name = "comboBoxComponent"; comboBoxComponent.Name = "comboBoxComponent";
comboBoxComponent.Size = new Size(121, 23); comboBoxComponent.Size = new Size(138, 28);
comboBoxComponent.TabIndex = 0; comboBoxComponent.TabIndex = 0;
comboBoxComponent.SelectedIndexChanged += comboBoxComponent_SelectedIndexChanged; 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 // VisualSelectionComponent
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
BorderStyle = BorderStyle.FixedSingle; BorderStyle = BorderStyle.FixedSingle;
Controls.Add(buttonClear);
Controls.Add(comboBoxComponent); Controls.Add(comboBoxComponent);
Margin = new Padding(3, 4, 3, 4);
Name = "VisualSelectionComponent"; Name = "VisualSelectionComponent";
Size = new Size(148, 94); Size = new Size(169, 87);
ResumeLayout(false); ResumeLayout(false);
} }
#endregion #endregion
private ComboBox comboBoxComponent; private ComboBox comboBoxComponent;
private Button buttonClear;
} }
} }

View File

@ -13,21 +13,11 @@ namespace ComponentsLab
{ {
get get
{ {
if (comboBoxComponent.SelectedItem != null) return comboBoxComponent.SelectedItem?.ToString() ?? string.Empty;
{
return comboBoxComponent.GetItemText(comboBoxComponent.SelectedItem);
}
else
{
return string.Empty;
}
} }
set set
{ {
if (comboBoxComponent.SelectedIndex != -1) comboBoxComponent.SelectedItem = value;
{
comboBoxComponent.Items[comboBoxComponent.SelectedIndex] = value;
}
} }
} }
@ -43,12 +33,9 @@ namespace ComponentsLab
} }
// Отдельный публичный метод отчистки списка // Отдельный публичный метод отчистки списка
private void buttonClear_Click(object sender, EventArgs e) public void clearList()
{ {
comboBoxComponent.Items.Clear(); comboBoxComponent.Items.Clear();
comboBoxComponent.Text = string.Empty;
comboBoxComponent.SelectedItem = null;
MessageBox.Show($"The list has been cleared");
} }

View File

@ -34,16 +34,16 @@
emailComponent = new ComponentsLibrary.Email(); emailComponent = new ComponentsLibrary.Email();
buttonCheckEmail = new Button(); buttonCheckEmail = new Button();
listBoxValues = new ComponentsLibrary.ListBoxValues(); listBoxValues = new ComponentsLibrary.ListBoxValues();
buttonGetListSelectedItem = new Button();
SuspendLayout(); SuspendLayout();
// //
// imageLoad // imageLoad
// //
imageLoad.Avatar = null; imageLoad.Avatar = null;
imageLoad.BorderStyle = BorderStyle.FixedSingle; imageLoad.BorderStyle = BorderStyle.FixedSingle;
imageLoad.Location = new Point(12, 12); imageLoad.Location = new Point(14, 16);
imageLoad.Margin = new Padding(3, 5, 3, 5);
imageLoad.Name = "imageLoad"; imageLoad.Name = "imageLoad";
imageLoad.Size = new Size(147, 92); imageLoad.Size = new Size(168, 122);
imageLoad.TabIndex = 0; imageLoad.TabIndex = 0;
imageLoad.AvatarChanged += ImageLoad_AvatarChanged; imageLoad.AvatarChanged += ImageLoad_AvatarChanged;
// //
@ -51,9 +51,10 @@
// //
visualSelectionComponent1.BorderStyle = BorderStyle.FixedSingle; visualSelectionComponent1.BorderStyle = BorderStyle.FixedSingle;
visualSelectionComponent1.comboBoxSelectedValue = ""; visualSelectionComponent1.comboBoxSelectedValue = "";
visualSelectionComponent1.Location = new Point(181, 12); visualSelectionComponent1.Location = new Point(207, 16);
visualSelectionComponent1.Margin = new Padding(3, 5, 3, 5);
visualSelectionComponent1.Name = "visualSelectionComponent1"; visualSelectionComponent1.Name = "visualSelectionComponent1";
visualSelectionComponent1.Size = new Size(156, 92); visualSelectionComponent1.Size = new Size(178, 122);
visualSelectionComponent1.TabIndex = 1; visualSelectionComponent1.TabIndex = 1;
visualSelectionComponent1.ChangeComboBox += visualSelectionComponent1_ChangeComboBox; visualSelectionComponent1.ChangeComboBox += visualSelectionComponent1_ChangeComboBox;
// //
@ -61,17 +62,19 @@
// //
emailComponent.BorderStyle = BorderStyle.FixedSingle; emailComponent.BorderStyle = BorderStyle.FixedSingle;
emailComponent.EmailPattern = null; emailComponent.EmailPattern = null;
emailComponent.Location = new Point(356, 12); emailComponent.Location = new Point(407, 16);
emailComponent.Margin = new Padding(3, 5, 3, 5);
emailComponent.Name = "emailComponent"; emailComponent.Name = "emailComponent";
emailComponent.Size = new Size(172, 92); emailComponent.Size = new Size(196, 122);
emailComponent.TabIndex = 2; emailComponent.TabIndex = 2;
emailComponent.ChangeEmail += emailComponent_ChangeEmail; emailComponent.ChangeEmail += emailComponent_ChangeEmail;
// //
// buttonCheckEmail // buttonCheckEmail
// //
buttonCheckEmail.Location = new Point(374, 66); buttonCheckEmail.Location = new Point(427, 88);
buttonCheckEmail.Margin = new Padding(3, 4, 3, 4);
buttonCheckEmail.Name = "buttonCheckEmail"; buttonCheckEmail.Name = "buttonCheckEmail";
buttonCheckEmail.Size = new Size(136, 23); buttonCheckEmail.Size = new Size(155, 31);
buttonCheckEmail.TabIndex = 3; buttonCheckEmail.TabIndex = 3;
buttonCheckEmail.Text = "Проверить email"; buttonCheckEmail.Text = "Проверить email";
buttonCheckEmail.UseVisualStyleBackColor = true; buttonCheckEmail.UseVisualStyleBackColor = true;
@ -80,34 +83,26 @@
// listBoxValues // listBoxValues
// //
listBoxValues.BorderStyle = BorderStyle.FixedSingle; listBoxValues.BorderStyle = BorderStyle.FixedSingle;
listBoxValues.Location = new Point(12, 110); listBoxValues.Location = new Point(14, 147);
listBoxValues.Margin = new Padding(3, 5, 3, 5);
listBoxValues.Name = "listBoxValues"; listBoxValues.Name = "listBoxValues";
listBoxValues.SelectedIndex = -1; listBoxValues.SelectedIndex = -1;
listBoxValues.Size = new Size(516, 224); listBoxValues.Size = new Size(589, 298);
listBoxValues.TabIndex = 4; listBoxValues.TabIndex = 4;
listBoxValues.GetObject += listBoxValues_GetObject; listBoxValues.GetObject += listBoxValues_GetObject;
listBoxValues.Load += listBoxValues_Load; 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 // FormComponents
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(551, 376); ClientSize = new Size(630, 501);
Controls.Add(buttonGetListSelectedItem);
Controls.Add(listBoxValues); Controls.Add(listBoxValues);
Controls.Add(buttonCheckEmail); Controls.Add(buttonCheckEmail);
Controls.Add(emailComponent); Controls.Add(emailComponent);
Controls.Add(visualSelectionComponent1); Controls.Add(visualSelectionComponent1);
Controls.Add(imageLoad); Controls.Add(imageLoad);
Margin = new Padding(3, 4, 3, 4);
Name = "FormComponents"; Name = "FormComponents";
Text = "FormComponents"; Text = "FormComponents";
ResumeLayout(false); ResumeLayout(false);
@ -121,6 +116,5 @@
private ComponentsLibrary.Email emailComponent; private ComponentsLibrary.Email emailComponent;
private Button buttonCheckEmail; private Button buttonCheckEmail;
private ComponentsLibrary.ListBoxValues listBoxValues; private ComponentsLibrary.ListBoxValues listBoxValues;
private Button buttonGetListSelectedItem;
} }
} }

View File

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

View File

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