очень надеюсь, что это конец
This commit is contained in:
parent
c73d358184
commit
c23bc5c7c1
57
Components/CustomListBox.Designer.cs
generated
Normal file
57
Components/CustomListBox.Designer.cs
generated
Normal file
@ -0,0 +1,57 @@
|
||||
namespace Components
|
||||
{
|
||||
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.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
|
||||
listBox.FormattingEnabled = true;
|
||||
listBox.Location = new Point(9, 5);
|
||||
listBox.Name = "listBox";
|
||||
listBox.Size = new Size(814, 384);
|
||||
listBox.TabIndex = 0;
|
||||
//
|
||||
// CustomListBox
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
Controls.Add(listBox);
|
||||
Name = "CustomListBox";
|
||||
Size = new Size(828, 410);
|
||||
ResumeLayout(false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private ListBox listBox;
|
||||
}
|
||||
}
|
108
Components/CustomListBox.cs
Normal file
108
Components/CustomListBox.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Components
|
||||
{
|
||||
public partial class CustomListBox : UserControl
|
||||
{
|
||||
public CustomListBox()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private string template;
|
||||
private string start;
|
||||
private string end;
|
||||
|
||||
public void setTemplate(string _template, string _start, string _end)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_template) || string.IsNullOrEmpty(_start) || string.IsNullOrEmpty(_end))
|
||||
{
|
||||
throw new ArgumentNullException("Wrong template");
|
||||
}
|
||||
template = _template;
|
||||
start = _start;
|
||||
end = _end;
|
||||
}
|
||||
|
||||
public int SelectedRow
|
||||
{
|
||||
get
|
||||
{
|
||||
return listBox.SelectedIndex;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value < 0) return;
|
||||
listBox.SelectedIndex = value;
|
||||
}
|
||||
}
|
||||
|
||||
public T GetObjectFromStr<T>() where T : class, new()
|
||||
{
|
||||
if (listBox.SelectedIndex < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
string row = listBox.SelectedItem.ToString();
|
||||
T curObject = new T();
|
||||
StringBuilder sb = new StringBuilder(row);
|
||||
foreach (var property in typeof(T).GetProperties())
|
||||
{
|
||||
if (!property.CanWrite)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
int startBorder = sb.ToString().IndexOf(start);
|
||||
if (startBorder == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
int endBorder = sb.ToString().IndexOf(end, startBorder + 1);
|
||||
if (endBorder == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
string propertyValue = sb.ToString(startBorder + 1, endBorder - startBorder - 1);
|
||||
sb.Remove(0, endBorder + 1);
|
||||
property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType));
|
||||
}
|
||||
return curObject;
|
||||
}
|
||||
|
||||
public void FillProperty<T>(T dataObject, int rowIndex, string propertyName)
|
||||
{
|
||||
|
||||
while (listBox.Items.Count <= rowIndex)
|
||||
{
|
||||
listBox.Items.Add(template);
|
||||
}
|
||||
|
||||
string row = listBox.Items[rowIndex].ToString();
|
||||
PropertyInfo propertyInfo = dataObject.GetType().GetProperty(propertyName);
|
||||
|
||||
if (propertyInfo != null)
|
||||
{
|
||||
var propertyValue = propertyInfo.GetValue(dataObject);
|
||||
row = row.Replace($"{start}{propertyName}{end}", propertyValue.ToString());
|
||||
listBox.Items[rowIndex] = row;
|
||||
}
|
||||
}
|
||||
|
||||
public int CountRows()
|
||||
{
|
||||
return listBox.Items.Count;
|
||||
}
|
||||
}
|
||||
}
|
120
Components/CustomListBox.resx
Normal file
120
Components/CustomListBox.resx
Normal 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>
|
14
CustomComboBox/TestApp/Day.cs
Normal file
14
CustomComboBox/TestApp/Day.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TestApp
|
||||
{
|
||||
public class Day
|
||||
{
|
||||
public string Mood { get; set; }
|
||||
public DateTime Date { get; set; }
|
||||
}
|
||||
}
|
74
CustomComboBox/TestApp/Form1.Designer.cs
generated
74
CustomComboBox/TestApp/Form1.Designer.cs
generated
@ -33,12 +33,17 @@
|
||||
buttonClear = new Button();
|
||||
buttonGet = new Button();
|
||||
customTextBox = new Components.CustomTextBox();
|
||||
customListBox = new Components.CustomListBox();
|
||||
buttonDay = new Button();
|
||||
buttonAddDay = new Button();
|
||||
textBoxMood = new TextBox();
|
||||
label1 = new Label();
|
||||
SuspendLayout();
|
||||
//
|
||||
// customComboBox
|
||||
//
|
||||
customComboBox.BorderStyle = BorderStyle.FixedSingle;
|
||||
customComboBox.Location = new Point(0, 0);
|
||||
customComboBox.Location = new Point(12, 0);
|
||||
customComboBox.Name = "customComboBox";
|
||||
customComboBox.SelectedItem = "";
|
||||
customComboBox.Size = new Size(252, 91);
|
||||
@ -47,7 +52,7 @@
|
||||
//
|
||||
// buttonAdd
|
||||
//
|
||||
buttonAdd.Location = new Point(12, 97);
|
||||
buttonAdd.Location = new Point(5, 97);
|
||||
buttonAdd.Name = "buttonAdd";
|
||||
buttonAdd.Size = new Size(94, 29);
|
||||
buttonAdd.TabIndex = 1;
|
||||
@ -57,7 +62,7 @@
|
||||
//
|
||||
// buttonClear
|
||||
//
|
||||
buttonClear.Location = new Point(12, 132);
|
||||
buttonClear.Location = new Point(105, 97);
|
||||
buttonClear.Name = "buttonClear";
|
||||
buttonClear.Size = new Size(94, 29);
|
||||
buttonClear.TabIndex = 2;
|
||||
@ -67,7 +72,7 @@
|
||||
//
|
||||
// buttonGet
|
||||
//
|
||||
buttonGet.Location = new Point(12, 167);
|
||||
buttonGet.Location = new Point(205, 97);
|
||||
buttonGet.Name = "buttonGet";
|
||||
buttonGet.Size = new Size(94, 29);
|
||||
buttonGet.TabIndex = 3;
|
||||
@ -77,18 +82,69 @@
|
||||
//
|
||||
// customTextBox
|
||||
//
|
||||
customTextBox.Location = new Point(258, 0);
|
||||
customTextBox.BorderStyle = BorderStyle.FixedSingle;
|
||||
customTextBox.Location = new Point(12, 144);
|
||||
customTextBox.Name = "customTextBox";
|
||||
customTextBox.numberPattern = null;
|
||||
customTextBox.Size = new Size(258, 88);
|
||||
customTextBox.TabIndex = 4;
|
||||
customTextBox.ValueChangedEvent += customTextBox_ValueChanged;
|
||||
//
|
||||
// customListBox
|
||||
//
|
||||
customListBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
|
||||
customListBox.Location = new Point(296, 0);
|
||||
customListBox.Name = "customListBox";
|
||||
customListBox.SelectedRow = -1;
|
||||
customListBox.Size = new Size(803, 400);
|
||||
customListBox.TabIndex = 5;
|
||||
//
|
||||
// buttonDay
|
||||
//
|
||||
buttonDay.Location = new Point(205, 342);
|
||||
buttonDay.Name = "buttonDay";
|
||||
buttonDay.Size = new Size(94, 29);
|
||||
buttonDay.TabIndex = 6;
|
||||
buttonDay.Text = "Get day";
|
||||
buttonDay.UseVisualStyleBackColor = true;
|
||||
buttonDay.Click += buttonDay_Click;
|
||||
//
|
||||
// buttonAddDay
|
||||
//
|
||||
buttonAddDay.Location = new Point(205, 371);
|
||||
buttonAddDay.Name = "buttonAddDay";
|
||||
buttonAddDay.Size = new Size(94, 29);
|
||||
buttonAddDay.TabIndex = 7;
|
||||
buttonAddDay.Text = "Write";
|
||||
buttonAddDay.UseVisualStyleBackColor = true;
|
||||
buttonAddDay.Click += buttonAddDay_Click;
|
||||
//
|
||||
// textBoxMood
|
||||
//
|
||||
textBoxMood.Location = new Point(74, 371);
|
||||
textBoxMood.Name = "textBoxMood";
|
||||
textBoxMood.Size = new Size(125, 27);
|
||||
textBoxMood.TabIndex = 8;
|
||||
//
|
||||
// label1
|
||||
//
|
||||
label1.AutoSize = true;
|
||||
label1.Location = new Point(12, 378);
|
||||
label1.Name = "label1";
|
||||
label1.Size = new Size(52, 20);
|
||||
label1.TabIndex = 9;
|
||||
label1.Text = "Mood:";
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(800, 450);
|
||||
ClientSize = new Size(1111, 416);
|
||||
Controls.Add(label1);
|
||||
Controls.Add(textBoxMood);
|
||||
Controls.Add(buttonAddDay);
|
||||
Controls.Add(buttonDay);
|
||||
Controls.Add(customListBox);
|
||||
Controls.Add(customTextBox);
|
||||
Controls.Add(buttonGet);
|
||||
Controls.Add(buttonClear);
|
||||
@ -97,6 +153,7 @@
|
||||
Name = "Form1";
|
||||
Text = "Form1";
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -106,5 +163,10 @@
|
||||
private Button buttonClear;
|
||||
private Button buttonGet;
|
||||
private Components.CustomTextBox customTextBox;
|
||||
private Components.CustomListBox customListBox;
|
||||
private Button buttonDay;
|
||||
private Button buttonAddDay;
|
||||
private TextBox textBoxMood;
|
||||
private Label label1;
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ namespace TestApp
|
||||
InitializeComponent();
|
||||
FillCustomComboBox();
|
||||
FillCustomTextBox();
|
||||
FillCustomListBox();
|
||||
}
|
||||
|
||||
private void FillCustomComboBox()
|
||||
@ -37,7 +38,7 @@ namespace TestApp
|
||||
}
|
||||
|
||||
private void buttonGet_Click(object sender, EventArgs e)
|
||||
{
|
||||
{
|
||||
MessageBox.Show("Âûáðàííûé ýëåìåíò :" + customComboBox.SelectedItem.ToString());
|
||||
}
|
||||
|
||||
@ -46,7 +47,7 @@ namespace TestApp
|
||||
private void FillCustomTextBox()
|
||||
{
|
||||
customTextBox.numberPattern = @"\+\d\s\d{3}\s\d{3}\s\d{2}\s\d{2}$";
|
||||
customTextBox.textBoxNumber = "+7 953 982 67 8";
|
||||
customTextBox.textBoxNumber = "+7 953 982 67 85";
|
||||
}
|
||||
private void customTextBox_ValueChanged(object sender, EventArgs e)
|
||||
{
|
||||
@ -60,5 +61,41 @@ namespace TestApp
|
||||
customTextBox.textBoxNumber = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void FillCustomListBox()
|
||||
{
|
||||
Day day1 = new Day() { Mood = "{áîëü}", Date = DateTime.Now };
|
||||
Day day2 = new Day() { Mood = "{ðàäîñòü}", Date = DateTime.Now.AddDays(1.0) };
|
||||
|
||||
customListBox.setTemplate("Äîðîãîé äíåâíèê, ìíå íå ïîäîáðàòü ñëîâ ÷òîáû îïèñàòü âñþ {Mood}, ÷òî ÿ èñïûòàë ñåãîäíÿ; {{Date}}", "{", "}");
|
||||
|
||||
customListBox.FillProperty(day1, 0, "Mood");
|
||||
customListBox.FillProperty(day1, 0, "Date");
|
||||
|
||||
customListBox.FillProperty(day2, 1, "Mood");
|
||||
customListBox.FillProperty(day2, 1, "Date");
|
||||
}
|
||||
|
||||
private void buttonDay_Click(object sender, EventArgs e)
|
||||
{
|
||||
Day selectedDay = customListBox.GetObjectFromStr<Day>();
|
||||
MessageBox.Show($"there was a lot {selectedDay.Mood} in {selectedDay.Date}");
|
||||
}
|
||||
|
||||
private void buttonAddDay_Click(object sender, EventArgs e)
|
||||
{
|
||||
Day day = new();
|
||||
day.Date = DateTime.Now;
|
||||
if (string.IsNullOrEmpty(textBoxMood.Text))
|
||||
{
|
||||
MessageBox.Show("Write something!!!");
|
||||
return;
|
||||
}
|
||||
day.Mood = "{" + textBoxMood.Text + "}";
|
||||
int index = customListBox.CountRows();
|
||||
customListBox.FillProperty(day, index, "Mood");
|
||||
customListBox.FillProperty(day, index, "Date");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user