lab1 ready

This commit is contained in:
Илья 2024-09-21 11:26:26 +04:00
parent 6c59744189
commit cf9dc68b63
7 changed files with 338 additions and 41 deletions

View File

@ -1,4 +1,6 @@
namespace RodionovLibrary.VisualComponents using System.Reflection;
namespace RodionovLibrary.VisualComponents
{ {
public partial class ListBoxControl : UserControl public partial class ListBoxControl : UserControl
{ {
@ -31,5 +33,61 @@
_fromChar = fromChar; _fromChar = fromChar;
_toChar = toChar; _toChar = toChar;
} }
public T? GetObject<T>()
{
if (listBox.SelectedIndex == -1 || string.IsNullOrEmpty(_template) || !_fromChar.HasValue || !_toChar.HasValue)
throw new ArgumentException("Не хватает данных");
var type = typeof(T);
var properties = type.GetProperties();
var curObject = Activator.CreateInstance(type);
string[] wordsTemplate = _template.Split(' ');
string[] words = listBox.SelectedItem.ToString().Split(' ');
for (int i = 0; i < wordsTemplate.Length; i++)
{
string word = wordsTemplate[i];
if (word.Contains(_fromChar.Value) && word.Contains(_toChar.Value))
{
int startCharIndex = word.IndexOf(_fromChar.Value);
int endCharIndex = word.LastIndexOf(_toChar.Value);
string propertyName = word.Substring(startCharIndex + 1, endCharIndex - startCharIndex - 1);
var property = properties.FirstOrDefault(x => x.Name == propertyName);
if (property == null)
continue;
int extraCharsBefore = startCharIndex;
int extraCharsAfter = word.Length - endCharIndex - 1;
string propertyValue = words[i].Substring(extraCharsBefore,
words[i].Length - extraCharsBefore - extraCharsAfter);
property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType));
}
}
return (T?)curObject;
}
public void AddItems<T>(List<T> items)
where T : class
{
if (string.IsNullOrEmpty(_template) || !_fromChar.HasValue || !_toChar.HasValue)
throw new ArgumentException("Не хватает данных");
listBox.Items.Clear();
var type = typeof(T);
var properties = type.GetProperties();
foreach (T item in items)
{
string result = _template;
foreach (var property in properties)
{
string search = _fromChar.Value + property.Name + _toChar.Value;
result = result.Replace(search, property.GetValue(item)?.ToString() ?? "");
}
listBox.Items.Add(result);
}
}
} }
} }

View File

@ -21,18 +21,15 @@ namespace RodionovLibrary.VisualComponents
public string Value { public string Value {
get get
{ {
if (Pattern == null) if (string.IsNullOrEmpty(Pattern))
throw new NullPatternException("Не задан шаблон!"); throw new NullPatternException("Не задан шаблон");
if (!new Regex(Pattern).IsMatch(textBox.Text)) if (!new Regex(Pattern).IsMatch(textBox.Text))
throw new NotMatchPatternException("Введенное значение не соответствует шаблону!"); throw new NotMatchPatternException("Введенное значение не соответствует шаблону");
return textBox.Text; return textBox.Text;
} }
set set
{ {
if (Pattern == null) if (!string.IsNullOrEmpty(Pattern) && new Regex(Pattern).IsMatch(value))
throw new NullPatternException("Не задан шаблон!");
if (!new Regex(Pattern).IsMatch(value))
throw new NotMatchPatternException("Введенное значение не соответствует шаблону!");
textBox.Text = value; textBox.Text = value;
} }
} }
@ -55,7 +52,7 @@ namespace RodionovLibrary.VisualComponents
private void TextBox_MouseEnter(object sender, EventArgs e) private void TextBox_MouseEnter(object sender, EventArgs e)
{ {
toolTip.Show(tooltipText ?? "", textBox); toolTip.Show(tooltipText ?? "", textBox, 45, 20, 3000);
} }
} }
} }

View File

@ -28,12 +28,179 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container(); comboBoxControl = new RodionovLibrary.VisualComponents.ComboBoxControl();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; listBoxControl = new RodionovLibrary.VisualComponents.ListBoxControl();
this.ClientSize = new System.Drawing.Size(800, 450); textBoxControl = new RodionovLibrary.VisualComponents.TextBoxControl();
this.Text = "Form1"; buttonClear = new Button();
buttonGetComboBox = new Button();
buttonSetComboBox = new Button();
buttonGetTextBox = new Button();
buttonSetTextBox = new Button();
buttonSetWrongTextBox = new Button();
buttonGetObject = new Button();
buttonGetIndex = new Button();
buttonSetIndex = new Button();
SuspendLayout();
//
// comboBoxControl
//
comboBoxControl.AutoSize = true;
comboBoxControl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
comboBoxControl.Location = new Point(45, 27);
comboBoxControl.Margin = new Padding(3, 4, 3, 4);
comboBoxControl.Name = "comboBoxControl";
comboBoxControl.SelectedValue = "";
comboBoxControl.Size = new Size(210, 32);
comboBoxControl.TabIndex = 0;
//
// listBoxControl
//
listBoxControl.AutoSize = true;
listBoxControl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
listBoxControl.Location = new Point(45, 276);
listBoxControl.Margin = new Padding(3, 4, 3, 4);
listBoxControl.Name = "listBoxControl";
listBoxControl.SelectedIndex = -1;
listBoxControl.Size = new Size(647, 488);
listBoxControl.TabIndex = 1;
//
// textBoxControl
//
textBoxControl.AutoSize = true;
textBoxControl.AutoSizeMode = AutoSizeMode.GrowAndShrink;
textBoxControl.Location = new Point(446, 27);
textBoxControl.Margin = new Padding(3, 4, 3, 4);
textBoxControl.Name = "textBoxControl";
textBoxControl.Pattern = null;
textBoxControl.Size = new Size(246, 31);
textBoxControl.TabIndex = 2;
//
// buttonClear
//
buttonClear.Location = new Point(47, 66);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(94, 29);
buttonClear.TabIndex = 3;
buttonClear.Text = "Отчистка";
buttonClear.UseVisualStyleBackColor = true;
buttonClear.Click += ButtonClear_Click;
//
// buttonGetComboBox
//
buttonGetComboBox.Location = new Point(47, 114);
buttonGetComboBox.Name = "buttonGetComboBox";
buttonGetComboBox.Size = new Size(165, 29);
buttonGetComboBox.TabIndex = 4;
buttonGetComboBox.Text = "Получение значения";
buttonGetComboBox.UseVisualStyleBackColor = true;
buttonGetComboBox.Click += ButtonGetComboBox_Click;
//
// buttonSetComboBox
//
buttonSetComboBox.Location = new Point(47, 165);
buttonSetComboBox.Name = "buttonSetComboBox";
buttonSetComboBox.Size = new Size(165, 29);
buttonSetComboBox.TabIndex = 5;
buttonSetComboBox.Text = "Установка значения";
buttonSetComboBox.UseVisualStyleBackColor = true;
buttonSetComboBox.Click += ButtonSetComboBox_Click;
//
// buttonGetTextBox
//
buttonGetTextBox.Location = new Point(446, 66);
buttonGetTextBox.Name = "buttonGetTextBox";
buttonGetTextBox.Size = new Size(165, 29);
buttonGetTextBox.TabIndex = 6;
buttonGetTextBox.Text = "Получение значения";
buttonGetTextBox.UseVisualStyleBackColor = true;
buttonGetTextBox.Click += ButtonGetTextBox_Click;
//
// buttonSetTextBox
//
buttonSetTextBox.Location = new Point(446, 114);
buttonSetTextBox.Name = "buttonSetTextBox";
buttonSetTextBox.Size = new Size(165, 29);
buttonSetTextBox.TabIndex = 7;
buttonSetTextBox.Text = "Установка значения";
buttonSetTextBox.UseVisualStyleBackColor = true;
buttonSetTextBox.Click += ButtonSetTextBox_Click;
//
// buttonSetWrongTextBox
//
buttonSetWrongTextBox.Location = new Point(446, 165);
buttonSetWrongTextBox.Name = "buttonSetWrongTextBox";
buttonSetWrongTextBox.Size = new Size(246, 29);
buttonSetWrongTextBox.TabIndex = 8;
buttonSetWrongTextBox.Text = "Установка значения (неверное)";
buttonSetWrongTextBox.UseVisualStyleBackColor = true;
buttonSetWrongTextBox.Click += ButtonSetWrongTextBox_Click;
//
// buttonGetObject
//
buttonGetObject.Location = new Point(699, 463);
buttonGetObject.Name = "buttonGetObject";
buttonGetObject.Size = new Size(165, 29);
buttonGetObject.TabIndex = 9;
buttonGetObject.Text = "Получение объекта";
buttonGetObject.UseVisualStyleBackColor = true;
buttonGetObject.Click += ButtonGetObject_Click;
//
// buttonGetIndex
//
buttonGetIndex.Location = new Point(699, 516);
buttonGetIndex.Name = "buttonGetIndex";
buttonGetIndex.Size = new Size(165, 29);
buttonGetIndex.TabIndex = 10;
buttonGetIndex.Text = "Получение индекса";
buttonGetIndex.UseVisualStyleBackColor = true;
buttonGetIndex.Click += ButtonGetIndex_Click;
//
// buttonSetIndex
//
buttonSetIndex.Location = new Point(699, 569);
buttonSetIndex.Name = "buttonSetIndex";
buttonSetIndex.Size = new Size(165, 29);
buttonSetIndex.TabIndex = 11;
buttonSetIndex.Text = "Установка индекса";
buttonSetIndex.UseVisualStyleBackColor = true;
buttonSetIndex.Click += ButtonSetIndex_Click;
//
// FormTest
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(878, 787);
Controls.Add(buttonSetIndex);
Controls.Add(buttonGetIndex);
Controls.Add(buttonGetObject);
Controls.Add(buttonSetWrongTextBox);
Controls.Add(buttonSetTextBox);
Controls.Add(buttonGetTextBox);
Controls.Add(buttonSetComboBox);
Controls.Add(buttonGetComboBox);
Controls.Add(buttonClear);
Controls.Add(textBoxControl);
Controls.Add(listBoxControl);
Controls.Add(comboBoxControl);
Name = "FormTest";
Text = "FormTest";
ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
private RodionovLibrary.VisualComponents.ComboBoxControl comboBoxControl;
private RodionovLibrary.VisualComponents.ListBoxControl listBoxControl;
private RodionovLibrary.VisualComponents.TextBoxControl textBoxControl;
private Button buttonClear;
private Button buttonGetComboBox;
private Button buttonSetComboBox;
private Button buttonGetTextBox;
private Button buttonSetTextBox;
private Button buttonSetWrongTextBox;
private Button buttonGetObject;
private Button buttonGetIndex;
private Button buttonSetIndex;
} }
} }

View File

@ -5,6 +5,64 @@ namespace WinForms
public FormTest() public FormTest()
{ {
InitializeComponent(); InitializeComponent();
comboBoxControl.AddItems(new List<string> { "Çíà÷åíèå 1", "Çíà÷åíèå 2", "Çíà÷åíèå 3", "Çíà÷åíèå 4", "Çíà÷åíèå 5" });
textBoxControl.Pattern = @"^[a-z0-9._%+-]+\@([a-z0-9-]+\.)+[a-z]{2,4}$";
textBoxControl.SetTooltipText("example@gmail.com");
listBoxControl.SetParams("Èìÿ: {FirstName}, ôàìèëèÿ: {LastName}. {Gender} ({Age}) ëåò.", '{', '}');
listBoxControl.AddItems(new List<Person> { new() { FirstName = "Êèðèëë", LastName = "Ïåòðîâ", Age = 23, Gender = "ìóæ" },
new() { FirstName = "Ìàðèÿ", LastName = "Èâàíîâà", Age = 18, Gender = "æåí" },
new() { FirstName = "Åâà", LastName = "Ïàíôèëîâà", Age = 40, Gender = "æåí" } });
}
private void ButtonClear_Click(object sender, EventArgs e)
{
comboBoxControl.Clear();
}
private void ButtonGetComboBox_Click(object sender, EventArgs e)
{
MessageBox.Show(comboBoxControl.SelectedValue, "Ïîëó÷åííîå çíà÷åíèå");
}
private void ButtonSetComboBox_Click(object sender, EventArgs e)
{
comboBoxControl.SelectedValue = "Çíà÷åíèå 3";
}
private void ButtonGetTextBox_Click(object sender, EventArgs e)
{
MessageBox.Show(textBoxControl.Value, "Ïîëó÷åííîå çíà÷åíèå");
}
private void ButtonSetTextBox_Click(object sender, EventArgs e)
{
textBoxControl.Value = "forum98761@gmail.com";
}
private void ButtonSetWrongTextBox_Click(object sender, EventArgs e)
{
textBoxControl.Value = "smth";
}
private void ButtonGetObject_Click(object sender, EventArgs e)
{
Person? selectedPerson = listBoxControl.GetObject<Person>();
if (selectedPerson == null)
MessageBox.Show("Îáüåêò ïóñòîé");
MessageBox.Show($"Èìÿ: {selectedPerson?.FirstName}, Ôàìèëèÿ: {selectedPerson?.LastName}, " +
$"Âîçðàñò: {selectedPerson?.Age}, Ïîë: {selectedPerson?.Gender}");
}
private void ButtonGetIndex_Click(object sender, EventArgs e)
{
MessageBox.Show(listBoxControl.SelectedIndex.ToString(), "Ïîëó÷åííîå çíà÷åíèå");
}
private void ButtonSetIndex_Click(object sender, EventArgs e)
{
listBoxControl.SelectedIndex = 0;
} }
} }
} }

13
COP/WinForms/Person.cs Normal file
View File

@ -0,0 +1,13 @@
namespace WinForms
{
public class Person
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public int Age { get; set; }
public string Gender { get; set; } = string.Empty;
}
}

View File

@ -8,4 +8,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\RodionovLibrary\RodionovLibrary.csproj" />
</ItemGroup>
</Project> </Project>