Compare commits
2 Commits
f098199010
...
103dc552e0
Author | SHA1 | Date | |
---|---|---|---|
103dc552e0 | |||
53a4567a0d |
@ -69,7 +69,7 @@ namespace ComponentsLibrary
|
||||
{
|
||||
if (string.IsNullOrEmpty(emailPattern))
|
||||
{
|
||||
return true; // Если шаблон не задан, любая строка считается валидной.
|
||||
return true;
|
||||
}
|
||||
return Regex.IsMatch(email, emailPattern);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace ComponentsLibrary
|
||||
{
|
||||
@ -62,7 +63,7 @@ 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);
|
||||
}
|
||||
|
||||
@ -75,25 +76,71 @@ namespace ComponentsLibrary
|
||||
var item = listBox.SelectedItem;
|
||||
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();
|
||||
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);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,6 @@
|
||||
private void InitializeComponent()
|
||||
{
|
||||
comboBoxComponent = new ComboBox();
|
||||
buttonClear = new Button();
|
||||
SuspendLayout();
|
||||
//
|
||||
// comboBoxComponent
|
||||
@ -37,38 +36,27 @@
|
||||
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.Location = new Point(17, 31);
|
||||
comboBoxComponent.Margin = new Padding(3, 4, 3, 4);
|
||||
comboBoxComponent.Name = "comboBoxComponent";
|
||||
comboBoxComponent.Size = new Size(121, 23);
|
||||
comboBoxComponent.Size = new Size(138, 28);
|
||||
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);
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
BorderStyle = BorderStyle.FixedSingle;
|
||||
Controls.Add(buttonClear);
|
||||
Controls.Add(comboBoxComponent);
|
||||
Margin = new Padding(3, 4, 3, 4);
|
||||
Name = "VisualSelectionComponent";
|
||||
Size = new Size(148, 94);
|
||||
Size = new Size(169, 87);
|
||||
ResumeLayout(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private ComboBox comboBoxComponent;
|
||||
private Button buttonClear;
|
||||
}
|
||||
}
|
||||
|
@ -13,21 +13,11 @@ namespace ComponentsLab
|
||||
{
|
||||
get
|
||||
{
|
||||
if (comboBoxComponent.SelectedItem != null)
|
||||
{
|
||||
return comboBoxComponent.GetItemText(comboBoxComponent.SelectedItem);
|
||||
}
|
||||
else
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return comboBoxComponent.SelectedItem?.ToString() ?? string.Empty;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (comboBoxComponent.SelectedIndex != -1)
|
||||
{
|
||||
comboBoxComponent.Items[comboBoxComponent.SelectedIndex] = value;
|
||||
}
|
||||
comboBoxComponent.SelectedItem = value;
|
||||
}
|
||||
}
|
||||
|
||||
@ -43,12 +33,9 @@ namespace ComponentsLab
|
||||
}
|
||||
|
||||
// Отдельный публичный метод отчистки списка
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
|
42
ComponentsView/FormComponents.Designer.cs
generated
42
ComponentsView/FormComponents.Designer.cs
generated
@ -34,16 +34,16 @@
|
||||
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.Location = new Point(14, 16);
|
||||
imageLoad.Margin = new Padding(3, 5, 3, 5);
|
||||
imageLoad.Name = "imageLoad";
|
||||
imageLoad.Size = new Size(147, 92);
|
||||
imageLoad.Size = new Size(168, 122);
|
||||
imageLoad.TabIndex = 0;
|
||||
imageLoad.AvatarChanged += ImageLoad_AvatarChanged;
|
||||
//
|
||||
@ -51,9 +51,10 @@
|
||||
//
|
||||
visualSelectionComponent1.BorderStyle = BorderStyle.FixedSingle;
|
||||
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.Size = new Size(156, 92);
|
||||
visualSelectionComponent1.Size = new Size(178, 122);
|
||||
visualSelectionComponent1.TabIndex = 1;
|
||||
visualSelectionComponent1.ChangeComboBox += visualSelectionComponent1_ChangeComboBox;
|
||||
//
|
||||
@ -61,17 +62,19 @@
|
||||
//
|
||||
emailComponent.BorderStyle = BorderStyle.FixedSingle;
|
||||
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.Size = new Size(172, 92);
|
||||
emailComponent.Size = new Size(196, 122);
|
||||
emailComponent.TabIndex = 2;
|
||||
emailComponent.ChangeEmail += emailComponent_ChangeEmail;
|
||||
//
|
||||
// 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.Size = new Size(136, 23);
|
||||
buttonCheckEmail.Size = new Size(155, 31);
|
||||
buttonCheckEmail.TabIndex = 3;
|
||||
buttonCheckEmail.Text = "Проверить email";
|
||||
buttonCheckEmail.UseVisualStyleBackColor = true;
|
||||
@ -80,34 +83,26 @@
|
||||
// listBoxValues
|
||||
//
|
||||
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.SelectedIndex = -1;
|
||||
listBoxValues.Size = new Size(516, 224);
|
||||
listBoxValues.Size = new Size(589, 298);
|
||||
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);
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(551, 376);
|
||||
Controls.Add(buttonGetListSelectedItem);
|
||||
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);
|
||||
@ -121,6 +116,5 @@
|
||||
private ComponentsLibrary.Email emailComponent;
|
||||
private Button buttonCheckEmail;
|
||||
private ComponentsLibrary.ListBoxValues listBoxValues;
|
||||
private Button buttonGetListSelectedItem;
|
||||
}
|
||||
}
|
||||
|
@ -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}");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ComponentsLab
|
||||
{
|
||||
internal static class Program
|
||||
@ -8,6 +10,7 @@ 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();
|
||||
|
Loading…
Reference in New Issue
Block a user