вроде все

This commit is contained in:
ityurner02@mail.ru 2023-09-15 22:21:56 +04:00
parent 2a817ffd5d
commit 17102989ae
7 changed files with 392 additions and 0 deletions

View File

@ -0,0 +1,59 @@
namespace VisualComponentsLib
{
partial class MyListBoxObjects
{
/// <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()
{
this.listBoxObj = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBoxObj
//
this.listBoxObj.FormattingEnabled = true;
this.listBoxObj.HorizontalScrollbar = true;
this.listBoxObj.ItemHeight = 16;
this.listBoxObj.Location = new System.Drawing.Point(3, 3);
this.listBoxObj.Name = "listBoxObj";
this.listBoxObj.Size = new System.Drawing.Size(353, 116);
this.listBoxObj.TabIndex = 0;
//
// MyListBoxObjects
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.listBoxObj);
this.Name = "MyListBoxObjects";
this.Size = new System.Drawing.Size(359, 133);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox listBoxObj;
}
}

View File

@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace VisualComponentsLib
{
public partial class MyListBoxObjects : UserControl
{
//Макетная строка
private string layoutString;
//начальный символ для обнаружения свойств/полей
private string startSymbol;
//конечный символ для обнаружения свойств/полей
private string endSymbol;
public MyListBoxObjects()
{
InitializeComponent();
}
//Метод для установки информации о макетной строке и символах (начального и конечного)
public void SetLayoutInfo(string layout, string startS, string endS)
{
if (layout == null || startS == null || endS == null)
{
return;
}
layoutString = layout;
startSymbol = startS;
endSymbol = endS;
}
//свойство для получения и заполнения индекса выбранного элемента
public int SelectedIndex
{
get
{
if (listBoxObj.SelectedIndex == -1)
{
return -1;
}
return listBoxObj.SelectedIndex;
}
set
{
if (listBoxObj.SelectedItems.Count != 0)
{
listBoxObj.SelectedIndex = value;
}
}
}
//Публичный параметризованный метод для получения объекта из выбранной строки(создать объект и через рефлексию заполнить свойства его).
public T GetObjectFromStr<T>() where T : class, new()
{
string selStr = "";
if (listBoxObj.SelectedIndex != -1)
{
selStr = listBoxObj.SelectedItem.ToString();
}
T curObject = new T();
foreach (var pr in typeof(T).GetProperties())
{
if (!pr.CanWrite)
{
continue;
}
int borderOne = selStr.IndexOf(startSymbol);
StringBuilder sb = new StringBuilder(selStr);
sb[borderOne] = 'z';
selStr = sb.ToString();
int borderTwo = selStr.IndexOf(endSymbol);
if (borderOne == -1 || borderTwo == -1) break;
string propertyValue = selStr.Substring(borderOne + 1, borderTwo - borderOne - 1);
selStr = selStr.Substring(borderTwo + 1);
pr.SetValue(curObject, Convert.ChangeType(propertyValue, pr.PropertyType));
}
return curObject;
}
//параметризованный метод, у которого в передаваемых параметрах идет список объектов некого класса и через этот список идет заполнение ListBox;
public void AddInListBox<T>(List<T> objects)
{
if (layoutString == null || startSymbol == null || endSymbol == null)
{
MessageBox.Show("заполните информацию о макетной строке");
return;
}
if (!layoutString.Contains(startSymbol) || !layoutString.Contains(endSymbol))
{
MessageBox.Show("Макетная строка не содержит нужные элементы");
return;
}
foreach (var item in objects)
{
string str = layoutString;
foreach (var prop in item.GetType().GetProperties())
{
string str1 = $"{startSymbol}" + $"{prop.Name}" + $"{endSymbol}";
str = str.Replace(str1, $"{startSymbol}" + prop.GetValue(item).ToString() + $"{endSymbol}");
}
listBoxObj.Items.Add(str);
}
}
}
}

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

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VisualComponentsLib.Object
{
public class Student
{
public string Name { get; set; }
public string Group { get; set; }
public string Faculty { get; set; }
public int Course { get; set; }
public Student(string name, string group, string faculty, int course)
{
Name = name;
Group = group;
Faculty = faculty;
Course = course;
}
public Student()
{
}
}
}

View File

@ -54,6 +54,13 @@
<Compile Include="MyEmailTextBox.Designer.cs">
<DependentUpon>MyEmailTextBox.cs</DependentUpon>
</Compile>
<Compile Include="MyListBoxObjects.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="MyListBoxObjects.Designer.cs">
<DependentUpon>MyListBoxObjects.cs</DependentUpon>
</Compile>
<Compile Include="Object\Student.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Service Include="{94E38DFF-614B-4cbd-B67C-F211BB35CE8B}" />
</ItemGroup>
@ -64,6 +71,9 @@
<EmbeddedResource Include="MyEmailTextBox.resx">
<DependentUpon>MyEmailTextBox.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="MyListBoxObjects.resx">
<DependentUpon>MyListBoxObjects.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -39,6 +39,10 @@
buttonSetExample = new Button();
labelExample = new Label();
textBoxExample = new TextBox();
listBoxObj = new VisualComponentsLib.MyListBoxObjects();
buttonAddObjects = new Button();
buttonShowItem = new Button();
labelShowInput = new Label();
SuspendLayout();
//
// dropDownList
@ -143,6 +147,44 @@
textBoxExample.Size = new Size(188, 27);
textBoxExample.TabIndex = 6;
//
// listBoxObj
//
listBoxObj.Location = new Point(388, 13);
listBoxObj.Margin = new Padding(3, 4, 3, 4);
listBoxObj.Name = "listBoxObj";
listBoxObj.SelectedIndex = -1;
listBoxObj.Size = new Size(368, 159);
listBoxObj.TabIndex = 11;
//
// buttonAddObjects
//
buttonAddObjects.Location = new Point(388, 179);
buttonAddObjects.Name = "buttonAddObjects";
buttonAddObjects.Size = new Size(380, 29);
buttonAddObjects.TabIndex = 12;
buttonAddObjects.Text = "Добавить";
buttonAddObjects.UseVisualStyleBackColor = true;
buttonAddObjects.Click += buttonAddObjects_Click;
//
// buttonShowItem
//
buttonShowItem.Location = new Point(388, 310);
buttonShowItem.Name = "buttonShowItem";
buttonShowItem.Size = new Size(380, 29);
buttonShowItem.TabIndex = 13;
buttonShowItem.Text = "Получить";
buttonShowItem.UseVisualStyleBackColor = true;
buttonShowItem.Click += buttonShowItem_Click;
//
// labelShowInput
//
labelShowInput.AutoSize = true;
labelShowInput.Location = new Point(388, 273);
labelShowInput.Name = "labelShowInput";
labelShowInput.Size = new Size(54, 20);
labelShowInput.TabIndex = 14;
labelShowInput.Text = "Вывод";
//
// Form1
//
AutoScaleDimensions = new SizeF(8F, 20F);
@ -159,6 +201,10 @@
Controls.Add(buttonSetExample);
Controls.Add(labelShow);
Controls.Add(buttonShow);
Controls.Add(listBoxObj);
Controls.Add(buttonAddObjects);
Controls.Add(buttonShowItem);
Controls.Add(labelShowInput);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
@ -177,5 +223,9 @@
private Button buttonSetExample;
private Label labelShow;
private Button buttonShow;
private VisualComponentsLib.MyListBoxObjects listBoxObj;
private Button buttonAddObjects;
private Button buttonShowItem;
private Label labelShowInput;
}
}

View File

@ -1,15 +1,26 @@
using System.Security.Cryptography;
using VisualComponentsLib.Object;
namespace WinForms
{
public partial class Form1 : Form
{
List<string> list = new List<string>();
List<Student> students = new List<Student>();
public Form1()
{
list = new List<string>();
list.AddRange(new string[] { "õëåá", "ìîëîêî", "êîëáàñà" });
Student student1 = new Student("Âàñèëüåâ", "ÏÈáä-32", "ÔÈÑÒ", 3);
Student student2 = new Student("Èâàíîâ", "ÐÒáä-11", "ÐÒÔ", 1);
Student student3 = new Student("Ñìèðíîâà", "ËÌÊÊáä-41", "ÃÔ", 4);
students.Add(student1);
students.Add(student2);
students.Add(student3);
InitializeComponent();
dropDownList.LoadValues(new List<string>() { "ñîê", "ÿáëîêî", "ëóê" });
emailTextBox.Pattern = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$";
listBoxObj.SetLayoutInfo("Ôàìèëèÿ *Name* Ãðóïïà *Group* Ôàêóëüòåò *Faculty* Êóðñ *Course*", "*", "*");
}
private void buttonAdd_Click(object sender, EventArgs e)
{
@ -49,5 +60,16 @@ namespace WinForms
MessageBox.Show(ex.Message);
}
}
private void buttonAddObjects_Click(object sender, EventArgs e)
{
listBoxObj.AddInListBox<Student>(students);
}
private void buttonShowItem_Click(object sender, EventArgs e)
{
string str = listBoxObj.GetObjectFromStr<Student>().Name + " " + listBoxObj.GetObjectFromStr<Student>().Group + " " + listBoxObj.GetObjectFromStr<Student>().Faculty + " " + listBoxObj.GetObjectFromStr<Student>().Course;
labelShowInput.Text = str;
}
}
}