Доделал третью таску

This commit is contained in:
Artyom_Yashin 2024-09-07 12:03:19 +04:00
parent c3b6b08a4e
commit b5f9121ce7
6 changed files with 487 additions and 1 deletions

View File

@ -0,0 +1,57 @@
namespace Library_var_4_lab_1
{
partial class CustomListBox
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
listBox = new ListBox();
SuspendLayout();
//
// listBox
//
listBox.FormattingEnabled = true;
listBox.ItemHeight = 20;
listBox.Location = new Point(3, 3);
listBox.Name = "listBox";
listBox.Size = new Size(446, 144);
listBox.TabIndex = 0;
//
// CustomListBox
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(listBox);
Name = "CustomListBox";
Size = new Size(454, 188);
ResumeLayout(false);
}
#endregion
private ListBox listBox;
}
}

View File

@ -0,0 +1,87 @@
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Library_var_4_lab_1
{
public partial class CustomListBox : UserControl
{
private List<string> properties = new();
private List<List<object>> values = new List<List<object>>();
private string Template = string.Empty;
private Regex Regex = new(string.Empty);
public CustomListBox()
{
InitializeComponent();
}
public void SetTemplate(string StartSimbol, string EndSimbol, string template)
{
string pattern = "\\" + StartSimbol + "([^"+EndSimbol+"]*)\\" + EndSimbol;
Template = template;
Regex = new Regex(pattern);
properties.Clear();
listBox.Items.Clear();
values.Clear();
foreach (Match match in Regex.Matches(template))
properties.Add(match.Groups[1].Value);
for (int i = 0; i < properties.Count; i++)
{
values.Add(new List<object>());
}
}
public int SelectedIndex
{
get
{
return listBox.SelectedIndex;
}
set
{
listBox.SelectedIndex = value;
}
}
public void Add<T>(T Object)
{
List<string> tmpList = new();
tmpList.AddRange(properties);
string[] splitStr = Regex.Split(Template);
string resultString = string.Empty;
for(int i = 0; i < splitStr.Length-1; i += 2)
{
resultString = string.Concat(resultString, splitStr[i]);
if (tmpList.Count > 0)
{
var tmpValue = typeof(T)?.GetProperty(tmpList.First())?.GetValue(Object);
resultString = string.Concat(resultString, tmpValue?.ToString() ?? string.Empty);
values[properties.Count - tmpList.Count].Add(tmpValue ?? string.Empty);
tmpList.RemoveAt(0);
}
}
listBox.Items.Add(resultString);
}
public T GetSelected<T>() where T : class, new()
{
Type objectType = typeof(T);
T Object = new T();
for (int i = 0; i < properties.Count; i++)
{
var field = objectType.GetProperty(properties[i]);
field?.SetValue(Object, values[i][listBox.SelectedIndex]);
}
return Object;
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -40,6 +40,21 @@
buttonGetDate = new Button(); buttonGetDate = new Button();
buttonSetExample = new Button(); buttonSetExample = new Button();
buttonSetTemplate = new Button(); buttonSetTemplate = new Button();
customListBox1 = new Library_var_4_lab_1.CustomListBox();
textBoxInput = new TextBox();
textBoxEndSimbol = new TextBox();
textBoxStartSimbol = new TextBox();
textBoxName = new TextBox();
textBoxAge = new TextBox();
textBoxSurName = new TextBox();
label1 = new Label();
label2 = new Label();
label3 = new Label();
buttonSetPattern = new Button();
buttonGetIndex = new Button();
buttonSetItem = new Button();
buttonGetObject = new Button();
buttonSetIndex = new Button();
SuspendLayout(); SuspendLayout();
// //
// dropDownList1 // dropDownList1
@ -154,11 +169,153 @@
buttonSetTemplate.UseVisualStyleBackColor = true; buttonSetTemplate.UseVisualStyleBackColor = true;
buttonSetTemplate.Click += buttonSetTemplate_OnClick; buttonSetTemplate.Click += buttonSetTemplate_OnClick;
// //
// customListBox1
//
customListBox1.SelectedIndex = -1;
customListBox1.Location = new Point(529, 83);
customListBox1.Name = "customListBox1";
customListBox1.Size = new Size(463, 176);
customListBox1.TabIndex = 12;
//
// textBoxInput
//
textBoxInput.Location = new Point(529, 255);
textBoxInput.Name = "textBoxInput";
textBoxInput.Size = new Size(450, 27);
textBoxInput.TabIndex = 13;
//
// textBoxEndSimbol
//
textBoxEndSimbol.Location = new Point(946, 298);
textBoxEndSimbol.Name = "textBoxEndSimbol";
textBoxEndSimbol.Size = new Size(33, 27);
textBoxEndSimbol.TabIndex = 14;
//
// textBoxStartSimbol
//
textBoxStartSimbol.Location = new Point(880, 298);
textBoxStartSimbol.Name = "textBoxStartSimbol";
textBoxStartSimbol.Size = new Size(33, 27);
textBoxStartSimbol.TabIndex = 15;
//
// textBoxName
//
textBoxName.Location = new Point(529, 345);
textBoxName.Name = "textBoxName";
textBoxName.Size = new Size(161, 27);
textBoxName.TabIndex = 16;
//
// textBoxAge
//
textBoxAge.Location = new Point(529, 448);
textBoxAge.Name = "textBoxAge";
textBoxAge.Size = new Size(161, 27);
textBoxAge.TabIndex = 17;
//
// textBoxSurName
//
textBoxSurName.Location = new Point(529, 395);
textBoxSurName.Name = "textBoxSurName";
textBoxSurName.Size = new Size(161, 27);
textBoxSurName.TabIndex = 18;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(529, 322);
label1.Name = "label1";
label1.Size = new Size(39, 20);
label1.TabIndex = 19;
label1.Text = "Имя";
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(529, 375);
label2.Name = "label2";
label2.Size = new Size(73, 20);
label2.TabIndex = 20;
label2.Text = "Фамилия";
//
// label3
//
label3.AutoSize = true;
label3.Location = new Point(529, 425);
label3.Name = "label3";
label3.Size = new Size(64, 20);
label3.TabIndex = 21;
label3.Text = "Возраст";
//
// buttonSetPattern
//
buttonSetPattern.Location = new Point(701, 343);
buttonSetPattern.Name = "buttonSetPattern";
buttonSetPattern.Size = new Size(136, 29);
buttonSetPattern.TabIndex = 22;
buttonSetPattern.Text = "Задать шаблон";
buttonSetPattern.UseVisualStyleBackColor = true;
buttonSetPattern.Click += buttonSetPattern_OnClick;
//
// buttonGetIndex
//
buttonGetIndex.Location = new Point(843, 343);
buttonGetIndex.Name = "buttonGetIndex";
buttonGetIndex.Size = new Size(136, 29);
buttonGetIndex.TabIndex = 23;
buttonGetIndex.Text = "Получит индекс";
buttonGetIndex.UseVisualStyleBackColor = true;
buttonGetIndex.Click += buttonGetIndex_Click;
//
// buttonSetItem
//
buttonSetItem.Location = new Point(696, 446);
buttonSetItem.Name = "buttonSetItem";
buttonSetItem.Size = new Size(141, 29);
buttonSetItem.TabIndex = 24;
buttonSetItem.Text = "Добавить пункт";
buttonSetItem.UseVisualStyleBackColor = true;
buttonSetItem.Click += buttonSetObject_OnClick;
//
// buttonGetObject
//
buttonGetObject.Location = new Point(843, 446);
buttonGetObject.Name = "buttonGetObject";
buttonGetObject.Size = new Size(136, 29);
buttonGetObject.TabIndex = 25;
buttonGetObject.Text = "Получить объект";
buttonGetObject.UseVisualStyleBackColor = true;
buttonGetObject.Click += buttonGetObject_OnClick;
//
// buttonSetIndex
//
buttonSetIndex.Location = new Point(843, 378);
buttonSetIndex.Name = "buttonSetIndex";
buttonSetIndex.Size = new Size(136, 29);
buttonSetIndex.TabIndex = 26;
buttonSetIndex.Text = "Задать индекс";
buttonSetIndex.UseVisualStyleBackColor = true;
buttonSetIndex.Click += buttonSetIndex_Click;
//
// Form1 // Form1
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(922, 549); ClientSize = new Size(1007, 549);
Controls.Add(buttonSetIndex);
Controls.Add(buttonGetObject);
Controls.Add(buttonSetItem);
Controls.Add(buttonGetIndex);
Controls.Add(buttonSetPattern);
Controls.Add(label3);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(textBoxSurName);
Controls.Add(textBoxAge);
Controls.Add(textBoxName);
Controls.Add(textBoxStartSimbol);
Controls.Add(textBoxEndSimbol);
Controls.Add(textBoxInput);
Controls.Add(customListBox1);
Controls.Add(buttonSetTemplate); Controls.Add(buttonSetTemplate);
Controls.Add(buttonSetExample); Controls.Add(buttonSetExample);
Controls.Add(buttonGetDate); Controls.Add(buttonGetDate);
@ -191,5 +348,20 @@
private Button buttonGetDate; private Button buttonGetDate;
private Button buttonSetExample; private Button buttonSetExample;
private Button buttonSetTemplate; private Button buttonSetTemplate;
private Library_var_4_lab_1.CustomListBox customListBox1;
private TextBox textBoxInput;
private TextBox textBoxEndSimbol;
private TextBox textBoxStartSimbol;
private TextBox textBoxName;
private TextBox textBoxAge;
private TextBox textBoxSurName;
private Label label1;
private Label label2;
private Label label3;
private Button buttonSetPattern;
private Button buttonGetIndex;
private Button buttonSetItem;
private Button buttonGetObject;
private Button buttonSetIndex;
} }
} }

View File

@ -12,6 +12,7 @@ namespace TestProj
dateInputBox1.ChangeValue += IsChanged; dateInputBox1.ChangeValue += IsChanged;
} }
//ïåðâàÿ òàñêà
private void buttonAdd_OnClick(object sender, EventArgs e) private void buttonAdd_OnClick(object sender, EventArgs e)
{ {
try try
@ -42,6 +43,7 @@ namespace TestProj
checkBox.Checked = true; checkBox.Checked = true;
} }
// âòîðàÿ òàñêà
private void buttonSetTemplate_OnClick(object sender, EventArgs e) private void buttonSetTemplate_OnClick(object sender, EventArgs e)
{ {
dateInputBox1.Template = textBox1.Text; dateInputBox1.Template = textBox1.Text;
@ -60,5 +62,38 @@ namespace TestProj
{ {
textBox1.Text = dateInputBox1.Date; textBox1.Text = dateInputBox1.Date;
} }
// òðåòüÿ òàñêà
private void buttonSetPattern_OnClick(object sender, EventArgs e)
{
customListBox1.SetTemplate(textBoxStartSimbol.Text, textBoxEndSimbol.Text, textBoxInput.Text);
}
private void buttonSetObject_OnClick(object sender, EventArgs e)
{
Human human = new Human();
human.Name = textBoxName.Text;
human.SurName = textBoxSurName.Text;
human.Age = Convert.ToInt32(textBoxAge.Text);
customListBox1.Add<Human>(human);
}
private void buttonGetObject_OnClick(object sender, EventArgs e)
{
Human human = customListBox1.GetSelected<Human>();
textBoxName.Text = human.Name;
textBoxSurName.Text = human.SurName;
textBoxAge.Text = human.Age.ToString();
}
private void buttonSetIndex_Click(object sender, EventArgs e)
{
customListBox1.SelectedIndex = Convert.ToInt32(textBoxInput.Text);
}
private void buttonGetIndex_Click(object sender, EventArgs e)
{
textBoxInput.Text = customListBox1.SelectedIndex.ToString();
}
} }
} }

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestProj
{
public class Human
{
public string Name { get; set; } = string.Empty;
public string SurName { get; set; } = string.Empty;
public int? Age { get; set; }
}
}