Вроде

This commit is contained in:
Алексей Тихоненков 2024-09-05 15:05:04 +04:00
parent 5a901fc072
commit 079f196dcf
10 changed files with 496 additions and 103 deletions

View File

@ -32,22 +32,29 @@
buttonClear = new Button();
ButtonShowItems = new Button();
textBoxItems = new TextBox();
userDateTimePicker = new COP.UserDateTimePicker();
textBoxShowDate = new TextBox();
ButtonShowDate = new Button();
ButtonRangeSet = new Button();
userTreeView = new COP.UserTreeView();
SuspendLayout();
//
// userCheckedListBox
//
userCheckedListBox.Location = new Point(0, 0);
userCheckedListBox.Margin = new Padding(3, 2, 3, 2);
userCheckedListBox.Name = "userCheckedListBox";
userCheckedListBox.SelectedValue = "";
userCheckedListBox.Size = new Size(263, 170);
userCheckedListBox.Size = new Size(230, 128);
userCheckedListBox.TabIndex = 0;
userCheckedListBox.SelectedValueChanged += UserCheckedListBox_SelectedValueChanged;
//
// buttonClear
//
buttonClear.Location = new Point(0, 176);
buttonClear.Location = new Point(0, 132);
buttonClear.Margin = new Padding(3, 2, 3, 2);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(263, 29);
buttonClear.Size = new Size(230, 22);
buttonClear.TabIndex = 2;
buttonClear.Text = "Очистить";
buttonClear.UseVisualStyleBackColor = true;
@ -55,9 +62,10 @@
//
// ButtonShowItems
//
ButtonShowItems.Location = new Point(0, 211);
ButtonShowItems.Location = new Point(0, 158);
ButtonShowItems.Margin = new Padding(3, 2, 3, 2);
ButtonShowItems.Name = "ButtonShowItems";
ButtonShowItems.Size = new Size(263, 29);
ButtonShowItems.Size = new Size(230, 22);
ButtonShowItems.TabIndex = 3;
ButtonShowItems.Text = "Показать";
ButtonShowItems.UseVisualStyleBackColor = true;
@ -65,21 +73,74 @@
//
// textBoxItems
//
textBoxItems.Location = new Point(0, 246);
textBoxItems.Location = new Point(0, 184);
textBoxItems.Margin = new Padding(3, 2, 3, 2);
textBoxItems.Multiline = true;
textBoxItems.Name = "textBoxItems";
textBoxItems.Size = new Size(263, 87);
textBoxItems.Size = new Size(231, 66);
textBoxItems.TabIndex = 4;
//
// userDateTimePicker
//
userDateTimePicker.Location = new Point(258, 11);
userDateTimePicker.Margin = new Padding(3, 2, 3, 2);
userDateTimePicker.MaxValue = null;
userDateTimePicker.MinValue = null;
userDateTimePicker.Name = "userDateTimePicker";
userDateTimePicker.Size = new Size(222, 22);
userDateTimePicker.TabIndex = 5;
userDateTimePicker.ValueChanged += UserDateTimePicker_ValueChanged;
//
// textBoxShowDate
//
textBoxShowDate.Location = new Point(258, 58);
textBoxShowDate.Name = "textBoxShowDate";
textBoxShowDate.Size = new Size(222, 23);
textBoxShowDate.TabIndex = 6;
//
// ButtonShowDate
//
ButtonShowDate.Location = new Point(290, 105);
ButtonShowDate.Name = "ButtonShowDate";
ButtonShowDate.Size = new Size(168, 23);
ButtonShowDate.TabIndex = 7;
ButtonShowDate.Text = "Показать";
ButtonShowDate.UseVisualStyleBackColor = true;
ButtonShowDate.Click += ButtonShowDate_Click;
//
// ButtonRangeSet
//
ButtonRangeSet.Location = new Point(486, 47);
ButtonRangeSet.Name = "ButtonRangeSet";
ButtonRangeSet.Size = new Size(131, 43);
ButtonRangeSet.TabIndex = 8;
ButtonRangeSet.Text = "Установить диапозон";
ButtonRangeSet.UseVisualStyleBackColor = true;
ButtonRangeSet.Click += ButtonRangeSet_Click;
//
// userTreeView
//
userTreeView.Location = new Point(454, 158);
userTreeView.Name = "userTreeView";
userTreeView.Size = new Size(150, 95);
userTreeView.TabIndex = 9;
userTreeView.Load += UserTreeView_Load;
//
// FormTestComponents
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(700, 338);
Controls.Add(userTreeView);
Controls.Add(ButtonRangeSet);
Controls.Add(ButtonShowDate);
Controls.Add(textBoxShowDate);
Controls.Add(userDateTimePicker);
Controls.Add(textBoxItems);
Controls.Add(ButtonShowItems);
Controls.Add(buttonClear);
Controls.Add(userCheckedListBox);
Margin = new Padding(3, 2, 3, 2);
Name = "FormTestComponents";
Text = "FormTestComponents ";
ResumeLayout(false);
@ -92,5 +153,10 @@
private Button buttonClear;
private Button ButtonShowItems;
private TextBox textBoxItems;
private COP.UserDateTimePicker userDateTimePicker;
private TextBox textBoxShowDate;
private Button ButtonShowDate;
private Button ButtonRangeSet;
private COP.UserTreeView userTreeView;
}
}

View File

@ -1,3 +1,5 @@
using COP;
namespace TestsComponents
{
public partial class FormTestComponents : Form
@ -13,7 +15,9 @@ namespace TestsComponents
}
private void UserCheckedListBox_SelectedValueChanged(object sender, EventArgs e)
{
MessageBox.Show("Selected value changed");
}
private void buttonClear_Click(object sender, EventArgs e)
{
@ -24,6 +28,58 @@ namespace TestsComponents
textBoxItems.Text = userCheckedListBox.SelectedValue;
}
private void ButtonShowDate_Click(object sender, EventArgs e)
{
try
{
textBoxShowDate.Clear();
textBoxShowDate.Text = userDateTimePicker.SelectedValue.ToString();
}
catch (Exception ex)
{
MessageBox.Show("Îøèáêà");
}
}
private void UserDateTimePicker_ValueChanged(object sender, EventArgs e)
{
MessageBox.Show("Value changed");
DateTime? selectedDate = userDateTimePicker.SelectedValue;
}
private void ButtonRangeSet_Click(object sender, EventArgs e)
{
userDateTimePicker.MaxValue = new DateTime(2024, 9, 20);
userDateTimePicker.MinValue = new DateTime(2024,8,10);
}
private void UserTreeView_Load(object sender, EventArgs e)
{
List<Employee> employees = new List<Employee>
{
new Employee { Department = "Îòäåë1", Position = "Äîëæíîñòü1", Name = "Ñîòðóäíèê1" },
new Employee { Department = "Îòäåë2", Position = "Äîëæíîñòü2", Name = "Ñîòðóäíèê2" },
new Employee { Department = "Îòäåë1", Position = "Äîëæíîñòü1", Name = "Ñîòðóäíèê3" },
new Employee { Department = "Îòäåë2", Position = "Äîëæíîñòü3", Name = "Ñîòðóäíèê4" },
};
var Hierarchy = new List<string> { "Department", "Position", "Name" };
foreach (var prop in employees[0].GetType().GetProperties())
{
userTreeView.Hierarchy.Add(prop.Name);
}
try
{
userTreeView.PopulateTree(employees);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class Employee
{
public string? Department { get; set; }
public string? Position { get; set; }
public string? Name { get; set; }
}
}
}

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace COP.Exceptions
{
[Serializable]
public class NoRangeException : ApplicationException
public class NoRangeException : Exception
{
public NoRangeException() : base() { }
public NoRangeException(string message) : base(message) { }

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace COP.Exceptions
{
[Serializable]
public class NotIncludedInRangeException: ApplicationException
public class NotIncludedInRangeException: Exception
{
public NotIncludedInRangeException() : base() { }

View File

@ -1,6 +1,6 @@
namespace COP
{
partial class UserDateTimePickercs
partial class UserDateTimePicker
{
/// <summary>
/// Обязательная переменная конструктора.

View File

@ -11,13 +11,13 @@ using COP.Exceptions;
namespace COP
{
public partial class UserDateTimePickercs : UserControl
public partial class UserDateTimePicker : UserControl
{
private DateTime? minValue;
private DateTime? maxValue;
public event EventHandler? _valueChanged;
public UserDateTimePickercs()
public UserDateTimePicker()
{
InitializeComponent();
@ -44,8 +44,8 @@ namespace COP
else if (dateTimePicker.Value < minValue ||
dateTimePicker.Value > maxValue)
{
throw new NotIncludedInRangeException("Selected value is out of range.");
return null;
}
return dateTimePicker.Value;
@ -60,7 +60,8 @@ namespace COP
if (value < minValue || value > maxValue)
{
throw new NotIncludedInRangeException("Selected value is out of range.");
return;
}
dateTimePicker.Value = value.Value;

View File

@ -0,0 +1,55 @@
namespace COP
{
partial class UserTreeView
{
/// <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()
{
treeView = new TreeView();
SuspendLayout();
//
// treeView
//
treeView.Location = new Point(0, 0);
treeView.Name = "treeView";
treeView.Size = new Size(150, 97);
treeView.TabIndex = 0;
//
// UserTreeView
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(treeView);
Name = "UserTreeView";
Size = new Size(150, 95);
ResumeLayout(false);
}
#endregion
private TreeView treeView;
}
}

View File

@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace COP
{
public partial class UserTreeView : UserControl
{
public List<string> Hierarchy;
public UserTreeView()
{
InitializeComponent();
Hierarchy = new();
}
public int SelectedItemIndex
{
get
{
if (treeView.SelectedNode == null)
{
return -1;
}
return treeView.SelectedNode.Index;
}
}
public T? GetSelectedObject<T>() where T : class, new()
{
// параметризованный
//проверка есть или нет иерархии
TreeNode selectedNode = treeView.SelectedNode;
if (Hierarchy == null)
{
throw new Exception("Hierarchy is not set.");
}
if (selectedNode?.FirstNode != null)
{
throw new Exception("Not a last level node.");
}
T currentBranch = new T();
Hierarchy.Reverse();
foreach (var property in Hierarchy)
{
typeof(T).GetProperty(property).SetValue(currentBranch, Convert.ChangeType(selectedNode.Text, typeof(T).GetProperty(property).PropertyType));
selectedNode = selectedNode.Parent;
}
Hierarchy.Reverse();
return currentBranch;
}
public void PopulateTree<T>(List<T> items)
{
treeView.BeginUpdate();
treeView.Nodes.Clear();
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
if (Hierarchy == null)
{
throw new Exception("Hierarchy is not set.");
}
foreach (var item in items)
{
TreeNodeCollection treeViewNodes = treeView.Nodes;
foreach (var elem in Hierarchy)
{
bool flag = false;
for (int i = 0; i < treeViewNodes.Count; i++)
{
if (item.GetType().GetProperty(elem).GetValue(item).ToString() == treeViewNodes[i].Text)
{
treeViewNodes = treeViewNodes[i].Nodes;
flag = true;
break;
}
}
if (!flag)
{
var newNode = treeViewNodes.Add(item.GetType().GetProperty(elem).GetValue(item).ToString());
treeViewNodes = newNode.Nodes;
}
}
}
treeView.EndUpdate();
}
}
}

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>